React to Auth API changes.

This commit is contained in:
Chris Ross 2015-04-24 11:32:15 -07:00
parent a71dc5858d
commit 3b32348cec
4 changed files with 84 additions and 81 deletions

View File

@ -79,7 +79,7 @@ namespace Microsoft.AspNet.Identity
/// <returns></returns> /// <returns></returns>
public virtual async Task RefreshSignInAsync(TUser user) 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 properties = authResult?.Properties ?? new AuthenticationProperties();
var authenticationMethod = authResult?.Principal?.FindFirstValue(ClaimTypes.AuthenticationMethod); var authenticationMethod = authResult?.Principal?.FindFirstValue(ClaimTypes.AuthenticationMethod);
await SignInAsync(user, properties, authenticationMethod); await SignInAsync(user, properties, authenticationMethod);
@ -98,16 +98,16 @@ namespace Microsoft.AspNet.Identity
{ {
userPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod)); userPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod));
} }
Context.Response.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme, Context.Authentication.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
userPrincipal, userPrincipal,
authenticationProperties ?? new AuthenticationProperties()); authenticationProperties ?? new AuthenticationProperties());
} }
public virtual void SignOut() public virtual void SignOut()
{ {
Context.Response.SignOut(IdentityOptions.ApplicationCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.ApplicationCookieAuthenticationScheme);
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
Context.Response.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
} }
private async Task<bool> IsLockedOut(TUser user) private async Task<bool> IsLockedOut(TUser user)
@ -227,7 +227,7 @@ namespace Microsoft.AspNet.Identity
public virtual async Task<bool> IsTwoFactorClientRememberedAsync(TUser user) public virtual async Task<bool> IsTwoFactorClientRememberedAsync(TUser user)
{ {
var userId = await UserManager.GetUserIdAsync(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); 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 userId = await UserManager.GetUserIdAsync(user);
var rememberBrowserIdentity = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType); var rememberBrowserIdentity = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);
rememberBrowserIdentity.AddClaim(new Claim(ClaimTypes.Name, userId)); rememberBrowserIdentity.AddClaim(new Claim(ClaimTypes.Name, userId));
Context.Response.SignIn(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme, Context.Authentication.SignIn(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
new ClaimsPrincipal(rememberBrowserIdentity), new ClaimsPrincipal(rememberBrowserIdentity),
new AuthenticationProperties { IsPersistent = true }); new AuthenticationProperties { IsPersistent = true });
} }
public virtual Task ForgetTwoFactorClientAsync() public virtual Task ForgetTwoFactorClientAsync()
{ {
Context.Response.SignOut(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
return Task.FromResult(0); return Task.FromResult(0);
} }
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Identity
// Cleanup external cookie // Cleanup external cookie
if (twoFactorInfo.LoginProvider != null) if (twoFactorInfo.LoginProvider != null)
{ {
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
} }
if (rememberClient) if (rememberClient)
{ {
@ -330,24 +330,24 @@ namespace Microsoft.AspNet.Identity
public virtual IEnumerable<AuthenticationDescription> GetExternalAuthenticationSchemes() 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) public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string expectedXsrf = null)
{ {
var auth = await Context.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationScheme); var auth = await Context.Authentication.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationScheme);
if (auth == null || auth.Principal == null || auth.Properties.Dictionary == null || !auth.Properties.Dictionary.ContainsKey(LoginProviderKey)) if (auth == null || auth.Principal == null || auth.Properties.Items == null || !auth.Properties.Items.ContainsKey(LoginProviderKey))
{ {
return null; return null;
} }
if (expectedXsrf != null) if (expectedXsrf != null)
{ {
if (!auth.Properties.Dictionary.ContainsKey(XsrfKey)) if (!auth.Properties.Items.ContainsKey(XsrfKey))
{ {
return null; return null;
} }
var userId = auth.Properties.Dictionary[XsrfKey] as string; var userId = auth.Properties.Items[XsrfKey] as string;
if (userId != expectedXsrf) if (userId != expectedXsrf)
{ {
return null; return null;
@ -355,7 +355,7 @@ namespace Microsoft.AspNet.Identity
} }
var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier); 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) if (providerKey == null || provider == null)
{ {
return null; return null;
@ -366,10 +366,10 @@ namespace Microsoft.AspNet.Identity
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null) public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
{ {
var properties = new AuthenticationProperties { RedirectUri = redirectUrl }; var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
properties.Dictionary[LoginProviderKey] = provider; properties.Items[LoginProviderKey] = provider;
if (userId != null) if (userId != null)
{ {
properties.Dictionary[XsrfKey] = userId; properties.Items[XsrfKey] = userId;
} }
return properties; return properties;
} }
@ -384,14 +384,14 @@ namespace Microsoft.AspNet.Identity
{ {
// Store the userId for use after two factor check // Store the userId for use after two factor check
var userId = await UserManager.GetUserIdAsync(user); 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; return SignInResult.TwoFactorRequired;
} }
} }
// Cleanup external cookie // Cleanup external cookie
if (loginProvider != null) if (loginProvider != null)
{ {
Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme); Context.Authentication.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
} }
await SignInAsync(user, isPersistent, loginProvider); await SignInAsync(user, isPersistent, loginProvider);
return SignInResult.Success; return SignInResult.Success;
@ -399,7 +399,7 @@ namespace Microsoft.AspNet.Identity
private async Task<TwoFactorAuthenticationInfo> RetrieveTwoFactorInfoAsync() private async Task<TwoFactorAuthenticationInfo> RetrieveTwoFactorInfoAsync()
{ {
var result = await Context.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme); var result = await Context.Authentication.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
if (result?.Principal != null) if (result?.Principal != null)
{ {
return new TwoFactorAuthenticationInfo return new TwoFactorAuthenticationInfo
@ -416,7 +416,7 @@ namespace Microsoft.AspNet.Identity
var state = Resources.FormatLoggingResultMessageForUser(methodName, await UserManager.GetUserIdAsync(user)); var state = Resources.FormatLoggingResultMessageForUser(methodName, await UserManager.GetUserIdAsync(user));
return Logger?.BeginScope(state); return Logger?.BeginScope(state);
} }
internal static ClaimsPrincipal StoreTwoFactorInfo(string userId, string loginProvider) internal static ClaimsPrincipal StoreTwoFactorInfo(string userId, string loginProvider)
{ {
var identity = new ClaimsIdentity(IdentityOptions.TwoFactorUserIdCookieAuthenticationType); var identity = new ClaimsIdentity(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);

View File

@ -206,7 +206,7 @@ namespace Microsoft.AspNet.Identity.InMemory
} }
else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder)) 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); Describe(res, result);
} }
else if (req.Path == new PathString("/testpath") && testpath != null) else if (req.Path == new PathString("/testpath") && testpath != null)
@ -241,7 +241,7 @@ namespace Microsoft.AspNet.Identity.InMemory
} }
if (result != null && result.Properties != null) 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()) using (var memory = new MemoryStream())
{ {

View File

@ -26,9 +26,9 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
app.UseCookieAuthentication(); app.UseCookieAuthentication();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
response.Setup(r => r.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme, auth.Setup(a => a.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme,
It.IsAny<ClaimsPrincipal>(), It.IsAny<ClaimsPrincipal>(),
It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable(); It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
var contextAccessor = new Mock<IHttpContextAccessor>(); var contextAccessor = new Mock<IHttpContextAccessor>();
@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
// Assert // Assert
Assert.True(result.Succeeded); Assert.True(result.Succeeded);
context.VerifyAll(); context.VerifyAll();
response.VerifyAll(); auth.VerifyAll();
contextAccessor.VerifyAll(); contextAccessor.VerifyAll();
} }
} }

View File

@ -182,9 +182,9 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response, user.Id, isPersistent); SetupSignIn(auth, user.Id, isPersistent);
var logStore = new StringBuilder(); var logStore = new StringBuilder();
var helper = SetupSignInManager(manager.Object, context.Object, logStore); var helper = SetupSignInManager(manager.Object, context.Object, logStore);
var expectedScope = string.Format("{0} for {1}: {2}", "PasswordSignInAsync", "user", user.Id); 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)); Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Fact] [Fact]
@ -213,9 +213,9 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response, user.Id, false); SetupSignIn(auth, user.Id, false);
var helper = SetupSignInManager(manager.Object, context.Object); var helper = SetupSignInManager(manager.Object, context.Object);
// Act // Act
@ -225,7 +225,7 @@ namespace Microsoft.AspNet.Identity.Test
Assert.True(result.Succeeded); Assert.True(result.Succeeded);
manager.Verify(); manager.Verify();
context.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(); manager.Setup(m => m.ResetAccessFailedCountAsync(user)).ReturnsAsync(IdentityResult.Success).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
SetupSignIn(response); SetupSignIn(auth);
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
var helper = SetupSignInManager(manager.Object, context.Object); var helper = SetupSignInManager(manager.Object, context.Object);
// Act // Act
@ -253,7 +253,7 @@ namespace Microsoft.AspNet.Identity.Test
Assert.True(result.Succeeded); Assert.True(result.Succeeded);
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Theory] [Theory]
@ -280,11 +280,11 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.ResetAccessFailedCountAsync(user)).ReturnsAsync(IdentityResult.Success).Verifiable(); manager.Setup(m => m.ResetAccessFailedCountAsync(user)).ReturnsAsync(IdentityResult.Success).Verifiable();
} }
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
response.Setup(r => r.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme, auth.Setup(a => a.SignIn(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme,
It.Is<ClaimsPrincipal>(id => id.FindFirstValue(ClaimTypes.Name) == user.Id), It.Is<ClaimsPrincipal>(id => id.FindFirstValue(ClaimTypes.Name) == user.Id),
It.IsAny<AuthenticationProperties>())).Verifiable(); 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 logStore = new StringBuilder();
var helper = SetupSignInManager(manager.Object, context.Object, logStore); var helper = SetupSignInManager(manager.Object, context.Object, logStore);
var expectedScope = string.Format("{0} for {1}: {2}", "PasswordSignInAsync", "user", user.Id); 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)); Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Theory] [Theory]
@ -323,9 +323,9 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.FindByLoginAsync(loginProvider, providerKey)).ReturnsAsync(user).Verifiable(); manager.Setup(m => m.FindByLoginAsync(loginProvider, providerKey)).ReturnsAsync(user).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response, user.Id, isPersistent, loginProvider); SetupSignIn(auth, user.Id, isPersistent, loginProvider);
var logStore = new StringBuilder(); var logStore = new StringBuilder();
var helper = SetupSignInManager(manager.Object, context.Object, logStore); var helper = SetupSignInManager(manager.Object, context.Object, logStore);
var expectedScope = string.Format("{0} for {1}: {2}", "ExternalLoginSignInAsync", "user", user.Id); 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)); Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Theory] [Theory]
@ -353,6 +353,8 @@ namespace Microsoft.AspNet.Identity.Test
// Setup // Setup
var user = new TestUser { UserName = "Foo" }; var user = new TestUser { UserName = "Foo" };
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
var loginProvider = "loginprovider"; var loginProvider = "loginprovider";
var id = new ClaimsIdentity(); var id = new ClaimsIdentity();
if (externalLogin) if (externalLogin)
@ -361,7 +363,7 @@ namespace Microsoft.AspNet.Identity.Test
} }
var properties = new AuthenticationProperties { IsPersistent = isPersistent }; var properties = new AuthenticationProperties { IsPersistent = isPersistent };
var authResult = new AuthenticationResult(new ClaimsPrincipal(id), properties, new AuthenticationDescription()); 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 manager = SetupUserManager(user);
var signInManager = new Mock<SignInManager<TestUser>>(manager.Object, var signInManager = new Mock<SignInManager<TestUser>>(manager.Object,
new HttpContextAccessor { HttpContext = context.Object }, new HttpContextAccessor { HttpContext = context.Object },
@ -376,6 +378,7 @@ namespace Microsoft.AspNet.Identity.Test
// Assert // Assert
context.Verify(); context.Verify();
auth.Verify();
signInManager.Verify(); signInManager.Verify();
} }
@ -411,34 +414,34 @@ namespace Microsoft.AspNet.Identity.Test
} }
manager.Setup(m => m.VerifyTwoFactorTokenAsync(user, provider, code)).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.VerifyTwoFactorTokenAsync(user, provider, code)).ReturnsAsync(true).Verifiable();
var context = new Mock<HttpContext>(); 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 twoFactorInfo = new SignInManager<TestUser>.TwoFactorAuthenticationInfo { UserId = user.Id };
var loginProvider = "loginprovider"; var loginProvider = "loginprovider";
var id = SignInManager<TestUser>.StoreTwoFactorInfo(user.Id, externalLogin ? loginProvider : null); var id = SignInManager<TestUser>.StoreTwoFactorInfo(user.Id, externalLogin ? loginProvider : null);
var authResult = new AuthenticationResult(id, new AuthenticationProperties(), new AuthenticationDescription()); var authResult = new AuthenticationResult(id, new AuthenticationProperties(), new AuthenticationDescription());
if (externalLogin) if (externalLogin)
{ {
response.Setup(r => r.SignIn( auth.Setup(a => a.SignIn(
IdentityOptions.ApplicationCookieAuthenticationScheme, IdentityOptions.ApplicationCookieAuthenticationScheme,
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider
&& i.FindFirstValue(ClaimTypes.NameIdentifier) == user.Id), && i.FindFirstValue(ClaimTypes.NameIdentifier) == user.Id),
It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable(); 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 else
{ {
SetupSignIn(response, user.Id); SetupSignIn(auth, user.Id);
} }
if (rememberClient) if (rememberClient)
{ {
response.Setup(r => r.SignIn( auth.Setup(a => a.SignIn(
IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id
&& i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType), && i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType),
It.Is<AuthenticationProperties>(v => v.IsPersistent == true))).Verifiable(); It.Is<AuthenticationProperties>(v => v.IsPersistent == true))).Verifiable();
} }
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); auth.Setup(a => a.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable();
context.Setup(c => c.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).ReturnsAsync(authResult).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
var logStore = new StringBuilder(); var logStore = new StringBuilder();
var helper = SetupSignInManager(manager.Object, context.Object, logStore); var helper = SetupSignInManager(manager.Object, context.Object, logStore);
var expectedScope = string.Format("{0} for {1}: {2}", "TwoFactorSignInAsync", "user", user.Id); 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)); Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Fact] [Fact]
@ -463,9 +466,9 @@ namespace Microsoft.AspNet.Identity.Test
var user = new TestUser { UserName = "Foo" }; var user = new TestUser { UserName = "Foo" };
var manager = SetupUserManager(user); var manager = SetupUserManager(user);
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
response.Setup(r => r.SignIn( auth.Setup(a => a.SignIn(
IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme,
It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id It.Is<ClaimsPrincipal>(i => i.FindFirstValue(ClaimTypes.Name) == user.Id
&& i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType), && i.Identities.First().AuthenticationType == IdentityOptions.TwoFactorRememberMeCookieAuthenticationType),
@ -479,7 +482,7 @@ namespace Microsoft.AspNet.Identity.Test
// Assert // Assert
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Theory] [Theory]
@ -499,13 +502,13 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.IsLockedOutAsync(user)).ReturnsAsync(false).Verifiable(); manager.Setup(m => m.IsLockedOutAsync(user)).ReturnsAsync(false).Verifiable();
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response); SetupSignIn(auth);
var id = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType); var id = new ClaimsIdentity(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);
id.AddClaim(new Claim(ClaimTypes.Name, user.Id)); id.AddClaim(new Claim(ClaimTypes.Name, user.Id));
var authResult = new AuthenticationResult(new ClaimsPrincipal(id), new AuthenticationProperties(), new AuthenticationDescription()); 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); var helper = SetupSignInManager(manager.Object, context.Object);
// Act // Act
@ -515,7 +518,7 @@ namespace Microsoft.AspNet.Identity.Test
Assert.True(result.Succeeded); Assert.True(result.Succeeded);
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Theory] [Theory]
@ -526,11 +529,11 @@ namespace Microsoft.AspNet.Identity.Test
// Setup // Setup
var manager = MockHelpers.MockUserManager<TestUser>(); var manager = MockHelpers.MockUserManager<TestUser>();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
response.Setup(r => r.SignOut(authenticationScheme)).Verifiable(); auth.Setup(a => a.SignOut(authenticationScheme)).Verifiable();
response.Setup(r => r.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).Verifiable(); auth.Setup(a => a.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme)).Verifiable();
response.Setup(r => r.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable(); auth.Setup(a => a.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme)).Verifiable();
IdentityOptions.ApplicationCookieAuthenticationScheme = authenticationScheme; IdentityOptions.ApplicationCookieAuthenticationScheme = authenticationScheme;
var logStore = new StringBuilder(); var logStore = new StringBuilder();
var helper = SetupSignInManager(manager.Object, context.Object, logStore); var helper = SetupSignInManager(manager.Object, context.Object, logStore);
@ -540,7 +543,7 @@ namespace Microsoft.AspNet.Identity.Test
// Assert // Assert
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
[Fact] [Fact]
@ -627,12 +630,12 @@ namespace Microsoft.AspNet.Identity.Test
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
} }
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
if (confirmed) if (confirmed)
{ {
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response); SetupSignIn(auth);
} }
var identityOptions = new IdentityOptions(); var identityOptions = new IdentityOptions();
identityOptions.SignIn.RequireConfirmedEmail = true; identityOptions.SignIn.RequireConfirmedEmail = true;
@ -653,12 +656,12 @@ namespace Microsoft.AspNet.Identity.Test
manager.Verify(); manager.Verify();
context.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 => It.Is<ClaimsPrincipal>(id =>
(userId == null || id.FindFirstValue(ClaimTypes.NameIdentifier) == userId) && (userId == null || id.FindFirstValue(ClaimTypes.NameIdentifier) == userId) &&
(loginProvider == null || id.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider)), (loginProvider == null || id.FindFirstValue(ClaimTypes.AuthenticationMethod) == loginProvider)),
@ -675,12 +678,12 @@ namespace Microsoft.AspNet.Identity.Test
var manager = SetupUserManager(user); var manager = SetupUserManager(user);
manager.Setup(m => m.IsPhoneNumberConfirmedAsync(user)).ReturnsAsync(confirmed).Verifiable(); manager.Setup(m => m.IsPhoneNumberConfirmedAsync(user)).ReturnsAsync(confirmed).Verifiable();
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>(); var auth = new Mock<AuthenticationManager>();
if (confirmed) if (confirmed)
{ {
manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable(); manager.Setup(m => m.CheckPasswordAsync(user, "password")).ReturnsAsync(true).Verifiable();
context.Setup(c => c.Response).Returns(response.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
SetupSignIn(response); SetupSignIn(auth);
} }
var identityOptions = new IdentityOptions(); var identityOptions = new IdentityOptions();
@ -700,7 +703,7 @@ namespace Microsoft.AspNet.Identity.Test
Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope)); Assert.NotEqual(-1, logStore.ToString().IndexOf(expectedScope));
manager.Verify(); manager.Verify();
context.Verify(); context.Verify();
response.Verify(); auth.Verify();
} }
} }
} }