React to Auth API changes.
This commit is contained in:
parent
a71dc5858d
commit
3b32348cec
|
|
@ -79,7 +79,7 @@ namespace Microsoft.AspNet.Identity
|
|||
/// <returns></returns>
|
||||
public virtual async Task RefreshSignInAsync(TUser user)
|
||||
{
|
||||
var authResult = await Context.AuthenticateAsync(IdentityOptions.ApplicationCookieAuthenticationScheme);
|
||||
var authResult = await Context.Authentication.AuthenticateAsync(IdentityOptions.ApplicationCookieAuthenticationScheme);
|
||||
var properties = authResult?.Properties ?? new AuthenticationProperties();
|
||||
var authenticationMethod = authResult?.Principal?.FindFirstValue(ClaimTypes.AuthenticationMethod);
|
||||
await SignInAsync(user, properties, authenticationMethod);
|
||||
|
|
@ -98,16 +98,16 @@ namespace Microsoft.AspNet.Identity
|
|||
{
|
||||
userPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod));
|
||||
}
|
||||
Context.Response.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
Context.Authentication.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
userPrincipal,
|
||||
authenticationProperties ?? new AuthenticationProperties());
|
||||
}
|
||||
|
||||
public virtual void SignOut()
|
||||
{
|
||||
Context.Response.SignOut(IdentityOptions.ApplicationCookieAuthenticationScheme);
|
||||
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
Context.Response.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.ApplicationCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
|
||||
}
|
||||
|
||||
private async Task<bool> IsLockedOut(TUser user)
|
||||
|
|
@ -227,7 +227,7 @@ namespace Microsoft.AspNet.Identity
|
|||
public virtual async Task<bool> IsTwoFactorClientRememberedAsync(TUser user)
|
||||
{
|
||||
var userId = await UserManager.GetUserIdAsync(user);
|
||||
var result = await Context.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
|
||||
var result = await Context.Authentication.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
|
||||
return (result?.Principal != null && result.Principal.FindFirstValue(ClaimTypes.Name) == userId);
|
||||
}
|
||||
|
||||
|
|
@ -236,14 +236,14 @@ namespace Microsoft.AspNet.Identity
|
|||
var userId = await UserManager.GetUserIdAsync(user);
|
||||
var rememberBrowserIdentity = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);
|
||||
rememberBrowserIdentity.AddClaim(new Claim(ClaimTypes.Name, userId));
|
||||
Context.Response.SignIn(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
|
||||
Context.Authentication.SignIn(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
|
||||
new ClaimsPrincipal(rememberBrowserIdentity),
|
||||
new AuthenticationProperties { IsPersistent = true });
|
||||
}
|
||||
|
||||
public virtual Task ForgetTwoFactorClientAsync()
|
||||
{
|
||||
Context.Response.SignOut(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
|
|
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Identity
|
|||
// Cleanup external cookie
|
||||
if (twoFactorInfo.LoginProvider != null)
|
||||
{
|
||||
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
}
|
||||
if (rememberClient)
|
||||
{
|
||||
|
|
@ -330,24 +330,24 @@ namespace Microsoft.AspNet.Identity
|
|||
|
||||
public virtual IEnumerable<AuthenticationDescription> GetExternalAuthenticationSchemes()
|
||||
{
|
||||
return Context.GetAuthenticationSchemes().Where(d => !string.IsNullOrEmpty(d.Caption));
|
||||
return Context.Authentication.GetAuthenticationSchemes().Where(d => !string.IsNullOrEmpty(d.Caption));
|
||||
}
|
||||
|
||||
public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string expectedXsrf = null)
|
||||
{
|
||||
var auth = await Context.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
if (auth == null || auth.Principal == null || auth.Properties.Dictionary == null || !auth.Properties.Dictionary.ContainsKey(LoginProviderKey))
|
||||
var auth = await Context.Authentication.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
if (auth == null || auth.Principal == null || auth.Properties.Items == null || !auth.Properties.Items.ContainsKey(LoginProviderKey))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (expectedXsrf != null)
|
||||
{
|
||||
if (!auth.Properties.Dictionary.ContainsKey(XsrfKey))
|
||||
if (!auth.Properties.Items.ContainsKey(XsrfKey))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var userId = auth.Properties.Dictionary[XsrfKey] as string;
|
||||
var userId = auth.Properties.Items[XsrfKey] as string;
|
||||
if (userId != expectedXsrf)
|
||||
{
|
||||
return null;
|
||||
|
|
@ -355,7 +355,7 @@ namespace Microsoft.AspNet.Identity
|
|||
}
|
||||
|
||||
var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var provider = auth.Properties.Dictionary[LoginProviderKey] as string;
|
||||
var provider = auth.Properties.Items[LoginProviderKey] as string;
|
||||
if (providerKey == null || provider == null)
|
||||
{
|
||||
return null;
|
||||
|
|
@ -366,10 +366,10 @@ namespace Microsoft.AspNet.Identity
|
|||
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
|
||||
{
|
||||
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
|
||||
properties.Dictionary[LoginProviderKey] = provider;
|
||||
properties.Items[LoginProviderKey] = provider;
|
||||
if (userId != null)
|
||||
{
|
||||
properties.Dictionary[XsrfKey] = userId;
|
||||
properties.Items[XsrfKey] = userId;
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
|
@ -384,14 +384,14 @@ namespace Microsoft.AspNet.Identity
|
|||
{
|
||||
// Store the userId for use after two factor check
|
||||
var userId = await UserManager.GetUserIdAsync(user);
|
||||
Context.Response.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme, StoreTwoFactorInfo(userId, loginProvider));
|
||||
Context.Authentication.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme, StoreTwoFactorInfo(userId, loginProvider));
|
||||
return SignInResult.TwoFactorRequired;
|
||||
}
|
||||
}
|
||||
// Cleanup external cookie
|
||||
if (loginProvider != null)
|
||||
{
|
||||
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
|
||||
}
|
||||
await SignInAsync(user, isPersistent, loginProvider);
|
||||
return SignInResult.Success;
|
||||
|
|
@ -399,7 +399,7 @@ namespace Microsoft.AspNet.Identity
|
|||
|
||||
private async Task<TwoFactorAuthenticationInfo> RetrieveTwoFactorInfoAsync()
|
||||
{
|
||||
var result = await Context.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
|
||||
var result = await Context.Authentication.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
|
||||
if (result?.Principal != null)
|
||||
{
|
||||
return new TwoFactorAuthenticationInfo
|
||||
|
|
@ -416,7 +416,7 @@ namespace Microsoft.AspNet.Identity
|
|||
var state = Resources.FormatLoggingResultMessageForUser(methodName, await UserManager.GetUserIdAsync(user));
|
||||
return Logger?.BeginScope(state);
|
||||
}
|
||||
|
||||
|
||||
internal static ClaimsPrincipal StoreTwoFactorInfo(string userId, string loginProvider)
|
||||
{
|
||||
var identity = new ClaimsIdentity(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ namespace Microsoft.AspNet.Identity.InMemory
|
|||
}
|
||||
else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder))
|
||||
{
|
||||
var result = await context.AuthenticateAsync(remainder.Value.Substring(1));
|
||||
var result = await context.Authentication.AuthenticateAsync(remainder.Value.Substring(1));
|
||||
Describe(res, result);
|
||||
}
|
||||
else if (req.Path == new PathString("/testpath") && testpath != null)
|
||||
|
|
@ -241,7 +241,7 @@ namespace Microsoft.AspNet.Identity.InMemory
|
|||
}
|
||||
if (result != null && result.Properties != null)
|
||||
{
|
||||
xml.Add(result.Properties.Dictionary.Select(extra => new XElement("extra", new XAttribute("type", extra.Key), new XAttribute("value", extra.Value))));
|
||||
xml.Add(result.Properties.Items.Select(extra => new XElement("extra", new XAttribute("type", extra.Key), new XAttribute("value", extra.Value))));
|
||||
}
|
||||
using (var memory = new MemoryStream())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -26,9 +26,9 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
|
|||
app.UseCookieAuthentication();
|
||||
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
response.Setup(r => r.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
auth.Setup(a => a.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
It.IsAny<ClaimsPrincipal>(),
|
||||
It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
|
||||
var contextAccessor = new Mock<IHttpContextAccessor>();
|
||||
|
|
@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
|
|||
// Assert
|
||||
Assert.True(result.Succeeded);
|
||||
context.VerifyAll();
|
||||
response.VerifyAll();
|
||||
auth.VerifyAll();
|
||||
contextAccessor.VerifyAll();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -182,9 +182,9 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response, user.Id, isPersistent);
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth, user.Id, isPersistent);
|
||||
var logStore = new StringBuilder();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object, logStore);
|
||||
var expectedScope = string.Format("{0} for {1}: {2}", "PasswordSignInAsync", "user", user.Id);
|
||||
|
|
@ -199,7 +199,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -213,9 +213,9 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response, user.Id, false);
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth, user.Id, false);
|
||||
var helper = SetupSignInManager(manager.Object, context.Object);
|
||||
|
||||
// Act
|
||||
|
|
@ -225,7 +225,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.True(result.Succeeded);
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -241,9 +241,9 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.ResetAccessFailedCountAsync(user)).ReturnsAsync(IdentityResult.Success).Verifiable();
|
||||
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
SetupSignIn(response);
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
SetupSignIn(auth);
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object);
|
||||
|
||||
// Act
|
||||
|
|
@ -253,7 +253,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.True(result.Succeeded);
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -280,11 +280,11 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.ResetAccessFailedCountAsync(user)).ReturnsAsync(IdentityResult.Success).Verifiable();
|
||||
}
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
response.Setup(r => r.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme,
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
auth.Setup(a => a.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme,
|
||||
It.Is<ClaimsPrincipal>(id => id.FindFirstValue(ClaimTypes.Name) == user.Id),
|
||||
It.IsAny<AuthenticationProperties>())).Verifiable();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
var logStore = new StringBuilder();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object, logStore);
|
||||
var expectedScope = string.Format("{0} for {1}: {2}", "PasswordSignInAsync", "user", user.Id);
|
||||
|
|
@ -300,7 +300,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -323,9 +323,9 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.FindByLoginAsync(loginProvider, providerKey)).ReturnsAsync(user).Verifiable();
|
||||
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response, user.Id, isPersistent, loginProvider);
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth, user.Id, isPersistent, loginProvider);
|
||||
var logStore = new StringBuilder();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object, logStore);
|
||||
var expectedScope = string.Format("{0} for {1}: {2}", "ExternalLoginSignInAsync", "user", user.Id);
|
||||
|
|
@ -340,7 +340,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -353,6 +353,8 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
// Setup
|
||||
var user = new TestUser { UserName = "Foo" };
|
||||
var context = new Mock<HttpContext>();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
var loginProvider = "loginprovider";
|
||||
var id = new ClaimsIdentity();
|
||||
if (externalLogin)
|
||||
|
|
@ -361,7 +363,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
}
|
||||
var properties = new AuthenticationProperties { IsPersistent = isPersistent };
|
||||
var authResult = new AuthenticationResult(new ClaimsPrincipal(id), properties, new AuthenticationDescription());
|
||||
context.Setup(c => c.AuthenticateAsync(IdentityOptions.ApplicationCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
auth.Setup(a => a.AuthenticateAsync(IdentityOptions.ApplicationCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
var manager = SetupUserManager(user);
|
||||
var signInManager = new Mock<SignInManager<TestUser>>(manager.Object,
|
||||
new HttpContextAccessor { HttpContext = context.Object },
|
||||
|
|
@ -376,6 +378,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
|
||||
// Assert
|
||||
context.Verify();
|
||||
auth.Verify();
|
||||
signInManager.Verify();
|
||||
}
|
||||
|
||||
|
|
@ -411,34 +414,34 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
}
|
||||
manager.Setup(m => m.VerifyTwoFactorTokenAsync(user, provider, code)).ReturnsAsync(true).Verifiable();
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
var twoFactorInfo = new SignInManager<TestUser>.TwoFactorAuthenticationInfo { UserId = user.Id };
|
||||
var loginProvider = "loginprovider";
|
||||
var id = SignInManager<TestUser>.StoreTwoFactorInfo(user.Id, externalLogin ? loginProvider : null);
|
||||
var authResult = new AuthenticationResult(id, new AuthenticationProperties(), new AuthenticationDescription());
|
||||
if (externalLogin)
|
||||
{
|
||||
response.Setup(r => r.SignIn(
|
||||
auth.Setup(a => a.SignIn(
|
||||
IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider
|
||||
&& i.FindFirstValue(ClaimTypes.NameIdentifier) == user.Id),
|
||||
It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
|
||||
response.Setup(r => r.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable();
|
||||
auth.Setup(a => a.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable();
|
||||
}
|
||||
else
|
||||
{
|
||||
SetupSignIn(response, user.Id);
|
||||
SetupSignIn(auth, user.Id);
|
||||
}
|
||||
if (rememberClient)
|
||||
{
|
||||
response.Setup(r => r.SignIn(
|
||||
auth.Setup(a => a.SignIn(
|
||||
IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
|
||||
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id
|
||||
&& i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType),
|
||||
It.Is<AuthenticationProperties>(v => v.IsPersistent == true))).Verifiable();
|
||||
}
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
context.Setup(c => c.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
auth.Setup(a => a.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
var logStore = new StringBuilder();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object, logStore);
|
||||
var expectedScope = string.Format("{0} for {1}: {2}", "TwoFactorSignInAsync", "user", user.Id);
|
||||
|
|
@ -453,7 +456,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -463,9 +466,9 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
var user = new TestUser { UserName = "Foo" };
|
||||
var manager = SetupUserManager(user);
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
response.Setup(r => r.SignIn(
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
auth.Setup(a => a.SignIn(
|
||||
IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
|
||||
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id
|
||||
&& i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType),
|
||||
|
|
@ -479,7 +482,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
// Assert
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -499,13 +502,13 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.IsLockedOutAsync(user)).ReturnsAsync(false).Verifiable();
|
||||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response);
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth);
|
||||
var id = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);
|
||||
id.AddClaim(new Claim(ClaimTypes.Name, user.Id));
|
||||
var authResult = new AuthenticationResult(new ClaimsPrincipal(id), new AuthenticationProperties(), new AuthenticationDescription());
|
||||
context.Setup(c => c.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
auth.Setup(a => a.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object);
|
||||
|
||||
// Act
|
||||
|
|
@ -515,7 +518,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.True(result.Succeeded);
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -526,11 +529,11 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
// Setup
|
||||
var manager = MockHelpers.MockUserManager<TestUser>();
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
response.Setup(r => r.SignOut(authenticationScheme)).Verifiable();
|
||||
response.Setup(r => r.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).Verifiable();
|
||||
response.Setup(r => r.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
auth.Setup(a => a.SignOut(authenticationScheme)).Verifiable();
|
||||
auth.Setup(a => a.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).Verifiable();
|
||||
auth.Setup(a => a.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable();
|
||||
IdentityOptions.ApplicationCookieAuthenticationScheme = authenticationScheme;
|
||||
var logStore = new StringBuilder();
|
||||
var helper = SetupSignInManager(manager.Object, context.Object, logStore);
|
||||
|
|
@ -540,7 +543,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
|
||||
// Assert
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -627,12 +630,12 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
}
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
if (confirmed)
|
||||
{
|
||||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response);
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth);
|
||||
}
|
||||
var identityOptions = new IdentityOptions();
|
||||
identityOptions.SignIn.RequireConfirmedEmail = true;
|
||||
|
|
@ -653,12 +656,12 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
|
||||
private static void SetupSignIn(Mock<HttpResponse> response, string userId = null, bool? isPersistent = null, string loginProvider = null)
|
||||
private static void SetupSignIn(Mock<AuthenticationManager> auth, string userId = null, bool? isPersistent = null, string loginProvider = null)
|
||||
{
|
||||
response.Setup(r => r.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
auth.Setup(a => a.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
|
||||
It.Is<ClaimsPrincipal>(id =>
|
||||
(userId == null || id.FindFirstValue(ClaimTypes.NameIdentifier) == userId) &&
|
||||
(loginProvider == null || id.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider)),
|
||||
|
|
@ -675,12 +678,12 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
var manager = SetupUserManager(user);
|
||||
manager.Setup(m => m.IsPhoneNumberConfirmedAsync(user)).ReturnsAsync(confirmed).Verifiable();
|
||||
var context = new Mock<HttpContext>();
|
||||
var response = new Mock<HttpResponse>();
|
||||
var auth = new Mock<AuthenticationManager>();
|
||||
if (confirmed)
|
||||
{
|
||||
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
|
||||
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
|
||||
SetupSignIn(response);
|
||||
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
|
||||
SetupSignIn(auth);
|
||||
}
|
||||
|
||||
var identityOptions = new IdentityOptions();
|
||||
|
|
@ -700,7 +703,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
|
||||
manager.Verify();
|
||||
context.Verify();
|
||||
response.Verify();
|
||||
auth.Verify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue