SPA 请求在大约 40 分钟不活动后失败



为什么我的请求在大约 40 分钟不活动后开始失败?

我设置了与 https://github.com/Azure-Samples/active-directory-dotnet-webapp-groupclaims 中相同的身份验证机制(我使用的是 Asp.Net MVC4:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = ConfigHelper.ClientId,
                Authority = ConfigHelper.Authority,
                PostLogoutRedirectUri = ConfigHelper.PostLogoutRedirectUri,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthorizationCodeReceived = async context =>
                    {
                        ClientCredential credential = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.AppKey);
                        string userObjectId = context.AuthenticationTicket.Identity.FindFirst(Globals.ObjectIdClaimType).Value;
                        AuthenticationContext authContext = new AuthenticationContext(ConfigHelper.Authority, new TokenDbCache(userObjectId));
                        AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                            context.Code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, ConfigHelper.GraphResourceId);
                    },
                    AuthenticationFailed = context =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("/Error/ShowError?signIn=true&errorMessage=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                }
            });
    }
}

我正在使用 angular2 SPA 应用程序,在一段时间不活动后,当应用程序尝试发送请求时,此请求失败( 在 chrome 工具中,我看到 API 请求被重定向到身份验证服务器 (azure aad),但它们没有被服务器正确处理,我在客户端看到身份验证服务器响应。)

如果我刷新页面,一切正常。

我已经禁用了IIS回收并设置了iddleTimout=0。

在 web.config 中,我禁用了默认身份验证:

<authentication mode="None" />
<sessionState timeout="1440">

事实证明,如果在OpenIdConnectAuthenticationOptions UseTokenLifetime设置为 true,cookie 过期时间设置将被忽略。我还发现我正在使用默认情况下此设置设置为 true 的库。此默认行为已更改,现在 UseTokenLifetime 默认设置为 false:https://github.com/aspnet/Security/commit/966fa6672ff3c5bd75703c325512778722ff46a2#diff-c130fbf3a8ec2c0c32beaf27fcf04ea9

所以

 new OpenIdConnectAuthenticationOptions
                { UseTokenLifetime = false }

解决了我的问题

最新更新