小编Jam*_*son的帖子

使用 Azure AD OpenIdConnect 和自定义中间件的 ASP NET Core 身份验证中的 Cookie 过期

目前,在通过 OpenIdConnect 身份验证模型使用 Azure AD 验证我的 .NET Core 应用程序时,我正在努力设置 cookie/auth 令牌的超时。

登录方案通过以下方式在ConfigureServices 方法中设置:

    services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
Run Code Online (Sandbox Code Playgroud)

然后我设置我的配置如下:

    app.UseCookieAuthentication(new CookieAuthenticationOptions()
    {
        CookieName = "MyCookie",
        ExpireTimeSpan = TimeSpan.FromHours(2)
    });

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions()
    {
        Authority = authorityUri.AbsoluteUri,
        ClientId = azureOptions.ClientId,
        ClientSecret = azureOptions.ClientSecret,
        ResponseType = OpenIdConnectResponseTypes.CodeIdToken,
        Events = new OpenIdConnectEvents()
        {
            OnAuthorizationCodeReceived = async context =>
            {
                await aAuthenticateMiddleware.OnAuthenticate(context, logger);
            }
        }
    });

    app.UseMiddleware<aAuthenticateMiddleware>();
Run Code Online (Sandbox Code Playgroud)

请注意,我没有使用内置的身份(因为它对我们的目的来说不实用),而是使用自定义中间件。

在中间件层中,我检查用户是否经过身份验证,如果没有发出质询:

    var authenticationProperties = new AuthenticationProperties() { RedirectUri = context.Request.Path.Value ?? "/" };
    authenticationProperties.AllowRefresh = false;
    authenticationProperties.IssuedUtc …
Run Code Online (Sandbox Code Playgroud)

c# authentication azure openid-connect asp.net-core-1.0

5
推荐指数
1
解决办法
3050
查看次数