【问题标题】:Renewing session using Azure AD authentication in ASP.NET MVC project在 ASP.NET MVC 项目中使用 Azure AD 身份验证更新会话
【发布时间】:2021-10-04 00:41:57
【问题描述】:

我已经使用 OpenId Connect 在 ASP.NET MVC 项目中配置了 Azure AD 身份验证。身份验证有效,但问题是,60 分钟后会话不再有效。在我的应用程序中,用户闲置一段时间是很常见的,并且应该能够继续工作而无需重新登录。这就是我在 Startup.auth.cs 中设置身份验证的方式:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions {
    CookieManager = new SystemWebCookieManager()
});

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = Authority,
        PostLogoutRedirectUri = postLogoutRedirectUri,

        Notifications = new OpenIdConnectAuthenticationNotifications()
        {
           AuthorizationCodeReceived = (context) => 
           {
               return System.Threading.Tasks.Task.FromResult(0);
           }
        }
    });

我已尝试按照https://www.cloudidentity.com/blog/2016/07/25/controlling-a-web-apps-session-duration-2/ 的建议添加会话刷新逻辑。这种方法的问题是登录窗口不能显示在框架中。

如何在用户关闭浏览器窗口之前保持会话有效?这似乎是一个常见问题,应该有一些常见的解决方案,但我找不到。

【问题讨论】:

    标签: c# .net asp.net-mvc azure-active-directory openid-connect


    【解决方案1】:

    您可以在代码中的“new OpenIdConnectAuthenticationOptions”部分下的代码中添加以下突出显示的参数:-

    app.SetDefaultSignInAsAuthenticationType
    (CookieAuthenticationDefaults.AuthenticationType);
    
    app.UseCookieAuthentication(new CookieAuthenticationOptions {
    CookieManager = new SystemWebCookieManager()});
    
    app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = Authority,
        PostLogoutRedirectUri = postLogoutRedirectUri,
    
        Notifications = new OpenIdConnectAuthenticationNotifications()
        {
           AuthorizationCodeReceived = (context) => 
           {
               return System.Threading.Tasks.Task.FromResult(0);
           }
        UseTokenLifetime = false
      }
     });'
    

    身份验证会话的生命周期(例如 cookie)应与身份验证令牌的生命周期相匹配。问题是您需要延长默认设置为一小时的 AAD 中的令牌生命周期。

    另外,请找到以下主题的链接以供参考:-

    AzureAD and OpenIdConnect session expiration in ASP.net WebForms

    Cookie expiry in ASP NET Core Authentication using Azure AD OpenIdConnect and custom middleware

    谢谢你,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多