从外部 URL 重定向回 ASP.NET MVC 中的操作方法后无法保留 cookie

Unable to persist cookies after redirecting from External URL back to Action Method in ASP.NET MVC

因此,作为我网站付款流程的一部分,我必须访问外部URL 来验证某些字段,作为验证完成的结果,我将获得 POST回到我的带有一些响应变量的操作方法。我面临的问题是,即使我尝试了以下步骤,cookie 仍无法持续存在。

  1. 已经为 cookie 明确分配了 SameSite 标志。
  2. 我已经对 Web.config 进行了一些更改,我将在下面包含这些内容。

我修改的部分Web.config。

  <system.web>
    <authentication mode="None">
      <forms cookieSameSite="Lax" requireSSL="false" />
    </authentication>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" executionTimeout="500" />
    
    <!-- Added this line for restoring Cookie values after the redirect to an external URI. -->
    <httpCookies requireSSL="true" />
    <sessionState cookieSameSite="None" cookieless="false" timeout="360" />    
  </system.web>

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647" />
      </webServices>
      <scriptResourceHandler enableCaching="false" enableCompression="false" />
    </scripting>
  </system.web.extensions>

 <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <!--<rewrite>
      <outboundRules>
        <clear />
        <rule name="Add SameSite" preCondition="No SameSite">
          <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
          <action type="Rewrite" value="{R:0}; SameSite=lax" />
        </rule>
        <preConditions>
          <preCondition name="No SameSite">
            <add input="{RESPONSE_Set_Cookie}" pattern="." />
            <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=lax" negate="true" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>-->
  </system.webServer>

我们调用外部URL的方法有这段代码。

HttpCookie ckpaymentTRID = new HttpCookie("PaResTransactionID");
ckpaymentTRID.Value = resultPaymentObj.TransactionID.ToString();
ckpaymentTRID.SameSite = System.Web.SameSiteMode.Lax;
ckpaymentTRID.Secure = true;

HttpContext.Response.Cookies.Add(ckpaymentTRID);

我从外部 URL 接收 POST 的方法包括这个

var SomeCookiee = HttpContext.Request.Cookies["PaResTransactionID"];

此外,我浏览了这篇文章 here 并了解 .NET 框架更新之前和 post 的变化。

在此先感谢您的帮助!!!

原来所有 web.config 设置更改都是无关紧要的,因为其余的实际上足以进行切割。这实际上是我如何获得有关如何解决此问题的提示:

当我从我的应用程序被重定向到 External-URL..... 在 Google Chrome 中,在开发工具下你可以看到已经传递的 cookie ...我总是收到一条警告说“由于您的 cookie 不是 secure cookie,chrome 默认情况下会更改 SameSite 设置从 NoneLax 所以你的 cookie 根本不会在整个请求中持续存在。”......然后提示我将 Web 应用程序设置更改为 运行 作为 https://localhost 而不是 VS2019 中的 http://localhost。一旦我这样做了,我发现我不再需要修改显式 HttpCookiesessionState 设置,或者实际上根本不需要放置在 web.config 并且 Cookie 值在外部域 Re-Direction.

的情况下仍然存在