【问题标题】:How to set SameSite value to None or Undefined for OWIN OpenIdConnect.Nonce cookie on .NET 4.7.2如何在 .NET 4.7.2 上为 OWIN OpenIdConnect.Nonce cookie 将 SameSite 值设置为 None 或 Undefined
【发布时间】:2020-05-17 01:18:25
【问题描述】:

我正在运行一个 ASP.NET MVC (4.7.2) Web 应用程序。我使用带有混合流的 Identity Server 4 实例进行外部身份验证。

在测试 Firefox 的新“缺少 SameSite 默认为 LAX”功能时,他没有将 LAX OpenIdConnect.Nonce cookie 发送回我的 MVC Web 应用程序:

图 1:所有 cookie 都有 Lax

图 2:在 POST 时未向 signin-oidc

提供 cookie

我得到错误:

OpenIdConnectProtocolValidationContext.Nonce 为空, OpenIdConnectProtocol.ValidatedIdToken.Payload.Nonce 不为空。这 nonce 无法验证。如果您不需要检查随机数,请设置 OpenIdConnectProtocolValidator.RequireNonce 为“假”。请注意,如果 找到'nonce',它将被评估。

我尝试了一些方法来确保所有 cookie 至少具有 None 或 Unspecified 设置,但是这个 OpenIdConnect.Nonce cookie 一直停留在 LAX。

app.UseKentorOwinCookieSaver();

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "Cookies",
    CookieSameSite = SameSiteMode.None,
    CookieManager = new SameSiteCookieManager(new SystemWebCookieManager())
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    Authority = configDetails.IdentityProviderUrl,
    AuthenticationType = "Cookies",
    ClientId = configDetails.ClientId,
    Scope = configDetails.Scope,
    ResponseType = "id_token code",
    RedirectUri = !string.IsNullOrWhiteSpace(configDetails.RedirectUri) ? configDetails.RedirectUri : "~/",
    SignInAsAuthenticationType = "Cookies",
    UseTokenLifetime = false,
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
        AuthorizationCodeReceived = AuthorizationCodeReceived,
        RedirectToIdentityProvider = n =>
        {
            if (n.ProtocolMessage.RequestType != Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectRequestType.Logout) return Task.FromResult(0);

            var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");
            if (idTokenHint != null)
            {
                n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
            }
            return Task.FromResult(0);
        }
    },
    CookieManager = new SameSiteCookieManager(new SystemWebCookieManager()),
});

在我的 web.config 中我添加了:

<system.web>
  <httpCookies sameSite="None" requireSSL="true" />
</system.web>

我正在使用来自Microsoft MSDN 的 SameSiteCookieManager。

我希望有人可以帮我将此 OpenIdConnect.Nonce cookie 设置为 None 或 Unspecified。

非常感谢!

【问题讨论】:

  • MS 提供了一个运行时补丁,可以改变 ASP.Net 中的 SameSite 行为。你在测试之前应用了这个吗?
  • @mackie 感谢您的回复。我们在 Azure 上托管,运行欧盟西部地区。根据这篇文章 (azure.microsoft.com/en-us/updates/…),该补丁应该在一月份在 Azure 上应用。但是,我们没有检查补丁版本。当然,我们正在处理的实例没有这个补丁(Azure 上的 86.0.7.90,需要的是 86.0.7.148)。可能就是这样!

标签: asp.net cookies owin identityserver4 samesite


【解决方案1】:

对于相同的站点 cookie 更改,我使用 IdSvr4 和 asp.net 4.7.2 进行了相同的设置。我的似乎现在正在工作。我使用了那个似乎可以解决问题的 cookie 管理器。我没有做 system.web httpCookie 元素。在这里查看我的帖子是否有帮助。

SO post on framework 4.7.2 and sameSite

【讨论】:

    【解决方案2】:

    我通过附加到 RedirectToIdentityProvider 并从那里手动设置它解决了 SameSite 问题。像这样的:

    RedirectToIdentityProvider = async n =>
    {
        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Authentication)
        {
            var nonceKey = HttpContext.Current.Response.Cookies.AllKeys.Where(x => x.Contains("nonce")).FirstOrDefault();
            if (nonceKey != null)
            {
                var nonce = HttpContext.Current.Response.Cookies.Get(nonceKey);
                nonce.SameSite = SameSiteMode.None
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2020-10-15
      • 1970-01-01
      • 2019-12-18
      • 2021-07-22
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多