Add SignOut to SignInManager

This commit is contained in:
Hao Kung 2014-04-15 11:21:22 -07:00
parent 538e8dabea
commit b079480383
4 changed files with 48 additions and 18 deletions

View File

@ -0,0 +1,7 @@
namespace Microsoft.AspNet.Identity.Security
{
public static class DefaultAuthenticationTypes
{
public const string ApplicationCookie = "Microsoft.Aspnet.Identity.Security.Application";
}
}

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Identity.Security
private string _authType;
public string AuthenticationType
{
get { return _authType ?? "Microsoft.AspNet.Identity"; }
get { return _authType ?? DefaultAuthenticationTypes.ApplicationCookie; }
set { _authType = value; }
}
@ -39,6 +39,15 @@ namespace Microsoft.AspNet.Identity.Security
Context.Response.SignIn(userIdentity, new AuthenticationProperties { IsPersistent = isPersistent });
}
public virtual void SignOut()
{
if (Context == null)
{
return;
}
Context.Response.SignOut(AuthenticationType);
}
//public virtual async Task<bool> SendTwoFactorCode(string provider)
//{
// var userId = await GetVerifiedUserId();

View File

@ -1600,22 +1600,6 @@ namespace Microsoft.AspNet.Identity.Entity.Test
Assert.False(await manager.VerifyTwoFactorTokenAsync(user, factorId, "bogus"));
}
private class DataStoreConfig : ContextConfiguration
{
private readonly DataStore _store;
public DataStoreConfig(DataStore store)
{
_store = store;
}
public override DataStore DataStore
{
get { return _store; }
}
}
public class TestMessageService : IIdentityMessageService
{
public IdentityMessage Message { get; set; }

View File

@ -65,7 +65,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
manager.Setup(m => m.IsLockedOutAsync(user, CancellationToken.None)).ReturnsAsync(false).Verifiable();
manager.Setup(m => m.FindByNameAsync(user.UserName, CancellationToken.None)).ReturnsAsync(user).Verifiable();
manager.Setup(m => m.CheckPasswordAsync(user, "password", CancellationToken.None)).ReturnsAsync(true).Verifiable();
manager.Setup(m => m.CreateIdentityAsync(user, "Microsoft.AspNet.Identity", CancellationToken.None)).ReturnsAsync(new ClaimsIdentity("Microsoft.AspNet.Identity")).Verifiable();
manager.Setup(m => m.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie, CancellationToken.None)).ReturnsAsync(new ClaimsIdentity("Microsoft.AspNet.Identity")).Verifiable();
var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
@ -80,6 +80,26 @@ namespace Microsoft.AspNet.Identity.Security.Test
manager.VerifyAll();
}
[Theory]
[InlineData(DefaultAuthenticationTypes.ApplicationCookie)]
[InlineData("Foo")]
public void SignOutCallsContextResponseSignOut(string authenticationType)
{
// Setup
var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
response.Setup(r => r.SignOut(authenticationType)).Verifiable();
var helper = new SignInManager<TestUser> { Context = context.Object, AuthenticationType = authenticationType };
// Act
helper.SignOut();
// Assert
context.VerifyAll();
response.VerifyAll();
}
[Fact]
public async Task PasswordSignInFailsWithWrongPassword()
{
@ -139,6 +159,16 @@ namespace Microsoft.AspNet.Identity.Security.Test
await helper.SignInAsync(null, false, false);
}
[Fact]
public void SignOutWithNoContextDoesNotBlowUp()
{
// Setup
var helper = new SignInManager<TestUser>();
// Act
helper.SignOut();
}
[Fact]
public async Task CreateUserIdentityReturnsNullNoUserManager()
{