我使用Asp.Net身份来控制我的应用程序的授权.现在,我需要这样做:如果用户在30分钟内没有操作,跳转到登录页面,当他登录时,没有选中"isPersistent"复选框.

public void ConfigureAuth(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        SlidingExpiration = true,
        CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
    });
}

登录代码如下所示:

private async Task SignInAsync(User user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    if (isPersistent)
    {
        AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
    }
    else
    {
        AuthenticationManager.SignIn(new AuthenticationProperties() { ExpiresUtc = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30)) }, identity);
    }
}

但我发现,当用户没有 Select iPersistent复选框时,cookies的过期日期已经是"会话",而不是当前时间加30分钟.

enter image description here

当使用类似after的代码时,cookies的状态会改变,因此"记住我"复选框无法工作:(.

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = TimeSpan.FromMinutes(30),
            SlidingExpiration = true,
            CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
        });

enter image description here

推荐答案

如果AuthenticationPropertiesIsPersistent属性设置为false,则cookie过期时间设置为Session.

如果checkbox"记住我"是checked,那么AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true }, userIdentity);将创建一个过期时间等于您在Startup.cs中设置的ExpireTimeSpan的cookie(缺省为14天).

如果checkbox"记住我"是NOT checked,那么你必须使用AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);.IsPersistent再次被设置为true,但现在我们给ExpiresUtc一个值,这样它就不用从CookieAuthenticationOptionsStartup.cs.

public override async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser)
{
    var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();
    // Clear any partial cookies from external or two factor partial sign ins
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
    if (rememberBrowser)
    {
        var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id));
        AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity);
    }
    else
    {
        //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity);
        if (isPersistent)
        {
            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity);
        }
        else
        {
            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30) }, userIdentity);
        }        
    }
}

Asp.net相关问答推荐

InvalidOperationException:在程序集上找不到UserSecretsIdAttribute

我可以在一个 Web 项目中有多个 web.config 文件吗?

IE10 SCRIPT5009:__doPostBack未定义

RestSharp 获取请求的完整 URL

在 RedirectToAction 调用中传播 QueryString 参数

不支持关键字:服务器

在属性中实现逻辑是一种好习惯吗

文本框的输入按键触发事件

没有回发的按钮?

如何从 JS 访问 ViewBag

测试 Web 表单应用程序 (ASP.NET) 的最佳方法是什么

与 Twitter Bootstrap Bundle 的 ASP.NET MVC4

单击按钮时播放哔声

在 ApiController 中添加自定义响应头

如何在 ASP.NET 中获取原始请求正文?

HttpResponse.End 或 HttpResponse.Close 与 HttpResponse.SuppressContent

web.config 中与 targetFramework 相关的配置错误

如何获取程序集的最后修改日期?

StyleBundle 索引超出了数组的范围

重新生成designer.cs