ASP.net MVC:身份和删除身份验证cookie

时间:2016-02-18 14:51:11

标签: asp.net-mvc cookies asp.net-identity

我从不使用身份。所以阅读一篇文章http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity

这是登录代码

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

    var identity = await UserManager.CreateIdentityAsync(
       user, DefaultAuthenticationTypes.ApplicationCookie);

    AuthenticationManager.SignIn(
       new AuthenticationProperties() { 
          IsPersistent = isPersistent 
       }, identity);
}

只是不明白上面代码行中的哪些代码删除了auth cookie?请告诉我。

当我们使用identity时,如何删除auth cookie,该cookie将在未来1或2个月内保留在用户pc中。告诉我如何设置auth cookie到期时间。请指导。感谢

1 个答案:

答案 0 :(得分:3)

您可以在启动时配置身份验证时设置Cookie的到期时间。

public partial class Startup {
    public void Configuration(IAppBuilder app) {
         ConfigureAuth(app);
    }

    public void ConfigureAuth(IAppBuilder app) {

        // This uses cookie to store information for the signed in user
        var authOptions = new CookieAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString(Constants.Paths.LoginPath), //Replace
            LogoutPath = new PathString(Constants.Paths.LogoutPath), //Replace
            //This sets the expiration of the cookie
            ExpireTimeSpan = System.TimeSpan.FromDays(60),   
        };            
        app.UseCookieAuthentication(authOptions);                      
    }
}