【问题标题】:ASP.NET_SessionId not found in OWIN OpenIdConnectAuthentication在 OWIN OpenIdConnectAuthentication 中找不到 ASP.NET_SessionId
【发布时间】:2020-09-24 20:54:39
【问题描述】:

BackGround : 用户一旦登录到我们的 Web 应用程序(使用应用程序级凭据),将看到他们想要使用的邮件系统,基于该用户将被重定向到相应的授权服务器以验证(使用他们邮件系统的登录名/密码),验证服务器将返回一个访问令牌。

在 OnAuthorizationCodeReceivedAsync 或 OnAuthenticationFailedAsync 等通知事件中;我们没有得到 ASP.NET_SessionId,所以我不能使用在 OAuth Flow 之前设置的任何会话值。

更多详情请参阅下面的代码。

app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With {
            .ClientId = appId,
            .ClientSecret = appSecret,
            .Authority = "https://login.microsoftonline.com/common/v2.0",
            .Scope = $"openid email profile offline_access {ewsScopes}",
            .RedirectUri = redirectUri,
            .PostLogoutRedirectUri = redirectUri,            
            .TokenValidationParameters = New TokenValidationParameters With {
                .ValidateIssuer = False
            },
            .Notifications = New OpenIdConnectAuthenticationNotifications With {
                .AuthenticationFailed = AddressOf OnAuthenticationFailedAsync,
                .AuthorizationCodeReceived = AddressOf OnAuthorizationCodeReceivedAsync
            }
        }) 

我无法在 HttpConext.Current.Session 中获取在通知事件中 OAuth 流之前设置的任何会话值。

根据以下 SO;我尝试了不同的方法,例如 SystemWebCookieManager、UseKentorOwinCookieSaver,但问题没有解决。
ASP.NET_SessionId + OWIN Cookies do not send to browser

可能是什么问题,我该如何解决?

【问题讨论】:

  • 我认为这个 SO 答案可能会对您有所帮助。 link
  • @Borka - 我试过了,但对我没用。

标签: asp.net asp.net-mvc asp.net-identity owin katana


【解决方案1】:

默认; OpenIDConnect 使用与 SameSite 不兼容的表单发布重定向。由于应用程序会话 cookie 没有发送过来,它应该是这样的。

根据下面的几个堆栈溢出链接;使用 URL 重写或以下 web.config 允许我们在响应回发回 Callback url 时保持会话,但我们仍然需要使用 Owin 的 SystemWebCookieManager 才能工作。

Browser won't set ASP.NET_SessionId cookie on payment gateway's post request to our site

how SameSite attribute added to my Asp.net_SessionID cookie automatically?

考虑到上述情况;用于 OpenIDConnect 身份验证;将 samesite cookie 设置为 none 且安全;这应该可行,但我担心这会引发应用程序的 CSRF(跨站点请求伪造)漏洞。

因此,另一种方法是切换到使用 HTTP 重定向并使用 SameSite=Lax 的代码响应类型。设置相应的代码响应模式和响应类型。

ResponseMode = OpenIdConnectResponseMode.Query;

ResponseType = OpenIdConnectResponseType.Code;

https://github.com/aspnet/AspNetKatana/blob/635c92f641ad1e014eead31cc7a365004949fda5/src/Microsoft.Owin.Security.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs#L65-L66

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 2015-05-29
    • 1970-01-01
    • 2017-11-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多