OWIN身份验证和超时

时间:2014-02-09 14:45:55

标签: owin

我正在使用带有OWIN身份验证的MVC 5。 以下是我的StartUp.cs的代码。

    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = new TimeSpan(60000000000)
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


    }

到期时间设置为60000000000纳秒。

现在要求是当cookie过期时,我需要重定向到登录屏幕。

怎么做?

2 个答案:

答案 0 :(得分:3)

希望这有助于某人调试...... 错误发生在web.config文件

<system.webServer>
  <modules>
    <remove name="FormsAuthenticationModule" />
  </modules>
<system.webServer>

这里名称Forms authenticationModule是一个错字。它应该是

<system.webServer>
  <modules>
    <remove name="FormsAuthentication" />
  </modules>
<system.webServer>

开始工作了。

答案 1 :(得分:0)

我发现this示例更好:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(15),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
    },
    SlidingExpiration = false,
    ExpireTimeSpan = TimeSpan.FromMinutes(30)
});

将上面的代码粘贴到App_Start文件夹中的Startup.Auth.cs文件中。