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

3

我从未使用过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);
}

不理解上述代码中哪一行代码会丢弃授权Cookie?请告诉我。

当我们使用身份验证时,如何设置一个在用户电脑上持续1或2个月的授权Cookie。请告诉我如何设置授权Cookie的过期时间。谢谢。


你所说的“drop”是什么意思?你是指创建cookie吗? - trailmax
1个回答

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);                      
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接