【问题标题】:Users get logged out every 10 to 20 minutes (claims authentication)用户每 10 到 20 分钟注销一次(声明身份验证)
【发布时间】:2017-05-24 20:31:58
【问题描述】:

由于某种原因,用户几乎每 10/20 分钟就会被重定向回 l​​ogin.microsoftonline.com。这很烦人,因为下面的代码用于登录 CMS 的用户。

谁能告诉我以下代码有什么问题以及为什么我们的用户会被注销/重定向回 l​​ogin.microsoftonline.com?会话生命周期设置为 60 分钟,因此它必须与授权本身有关。

我们应该使用 WsFederationAuthenticationDefaults.AuthenticationType、CookieAuthenticationDefaults.AuthenticationType 还是 DefaultAuthenticationTypes.ApplicationCookie?

我们希望允许用户使用表单 (/account/inloggen) 或使用名为“Azure SSO”的按钮(即外部登录)登录

public void ConfigureAuth(IAppBuilder app)
{
  // Configure the db context and user manager to use a single instance per request
  app.CreatePerOwinContext(ApplicationDbContext.Create);
  app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

  // Enable the application to use a cookie to store information for the signed in user
  // and to use a cookie to temporarily store information about a user logging in with a third party login provider
  // Configure the sign in cookie

  app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/account/inloggen"),
    Provider = new CookieAuthenticationProvider
    {
      OnResponseSignIn = ctx =>
      {
        ctx.Identity = TransformClaims(ctx.Identity);
        ctx.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7.0);
      },
      OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                              validateInterval: TimeSpan.FromMinutes(30),
                              regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    },
    ExpireTimeSpan = TimeSpan.FromDays(7.0),
    SlidingExpiration = true
  });

  app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
    Provider = new CookieAuthenticationProvider
    {
      OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
          validateInterval: TimeSpan.FromMinutes(30),
          regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    },
    ExpireTimeSpan = TimeSpan.FromDays(7.0),
    SlidingExpiration = true
  });

  app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
  {
    MetadataAddress = "https://login.microsoftonline.com/xxxxxxxxxxxxxx/federationmetadata.xml",
    Wtrealm = "https://portal.domain.com",
    Caption = "Azure SSO",
    SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
    UseTokenLifetime = false,
    AuthenticationMode = AuthenticationMode.Passive
  });

  app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

我们何时以及为什么要使用它?

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

您可能已经注意到,我对这一切都很陌生。我浏览了堆栈溢出并在 Google 上搜索了很多示例,但没有明确的答案/教程来解释不同的授权类型、它们的属性以及它们的使用方式。

【问题讨论】:

    标签: authorization single-sign-on claims-based-identity


    【解决方案1】:

    您的 validateInterval: TimeSpan.FromMinutes(30) 设置为 30 分钟。

    validateInterval 与在给定时间成名的过期 cookie 不同。 例如,用户在位置 A 登录,然后转到位置 B 并登录并更改密码。然后他们在 30 分钟后返回位置 A。他们将被注销。

    每当创建/更改密码或添加/删除外部登录时,都会创建 SecurityStampValidator。

    来源http://www.jamessturtevant.com/posts/ASPNET-Identity-Cookie-Authentication-Timeouts/

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答。但是,它设置为 30 分钟,当前用户每 10-20 分钟注销一次,而 SlidingExpiration 设置为 true。即使启用了 SlidingExpiration,用户怎么会被注销? regenerateIdentity 函数不会生成新的有效身份吗?我想不出别的了。
    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2021-05-24
    • 2013-04-05
    • 2015-05-25
    • 1970-01-01
    • 2012-02-13
    • 2011-06-03
    • 2010-11-07
    相关资源
    最近更新 更多