我使用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相关问答推荐

SignalR + Autofac + OWIN:为什么 GlobalHost.ConnectionManager.GetHubContext 不起作用?

为 Web 请求实现速率限制算法的最佳方法是什么?

如何为发布模式设置调试错误

有没有办法将 onclick 事件添加到 ASP.NET 标签服务器控件?

我可以根据角色隐藏/显示 asp:Menu 项吗?

如何在集线器类之外获取 SignalR 用户连接 ID?

Response.Redirect 使用 ~ 路径

Webapi、Webhost和Owin的关系

使用 Visual Studio 2012 恢复删除的文件

与将 Web 应用程序保存在一个默认应用程序池中相比,拥有专用应用程序池的优缺点

HttpContext.Current.Cache.Insert 和 HttpContext.Current.Cache.Add 有什么区别

ASP.NET RadioButton 与名称(组名)混淆

LINQ to Entities 无法识别 MVC 4 中的System.String ToString()方法

如何将图像 url 转换为 system.drawing.image

aspnet_compiler 发现错误版本的 System.Web.WebPages 1.0.0.0 而不是 2.0.0.0

如何获得 System.Diagnostics.Process 的输出?

@(at) 登录文件路径/字符串

VS 2010 中缺少 App_Code 文件夹

ASP.NET Web 窗体中的 jQuery 验证插件

静态变量是线程安全的吗? C#