简体   繁体   English

从第三方 API(支付网关)重定向后丢失 session 数据

[英]Losing session data after redirecting from a 3rd party API (payment gateway)

I'm working on an e-commerce site which is built on top of ASP.NET MVC .我正在开发一个建立在ASP.NET MVC之上的电子商务网站。 We are using 3rd party payment gateway for online payment transactions.我们使用第三方支付网关进行在线支付交易。 Basically, I'm redirecting the user to the payment gateway with a successUrl and a failUrl .基本上,我使用successUrlfailUrl将用户重定向到支付网关。 If everything goes okay then the payment gateway redirects the user to my successUrl .如果一切顺利,支付网关会将用户重定向到我的successUrl

The problem I'm facing is that I'm losing all session data as soon as the user is redirected to my successUrl .我面临的问题是,一旦用户被重定向到我的 successUrl ,我就会丢失所有successUrl数据。 So, I'm unable to track this user and I can't process the order further.因此,我无法跟踪此用户,也无法进一步处理该订单。 More details:更多细节:

  1. I'm using InProc SessionState我正在使用InProc SessionState
  2. I have defined timeout in SessionState but that doesn't help我在 SessionState 中定义了timeout ,但这没有帮助
  3. I've also defined Session.Timeout in the Session_Start method of Global.asax file我还在Global.asax文件的Session_Start方法中定义Session.Timeout
  4. Currently my application uses http and the payment gateway uses https目前我的应用程序使用http ,支付网关使用https
  5. Payment gateway is built on top of PHP支付网关建立在PHP

What I've tried:我试过的:

I've created a dummy API then sent a request to it from my e-commerce app and then redirected it to my e-commerce app.我创建了一个虚拟 API 然后从我的电子商务应用程序向它发送请求,然后将其重定向到我的电子商务应用程序。 In this case I don't lose my session data.在这种情况下,我不会丢失 session 数据。 So, I'm not sure what is wrong here.所以,我不确定这里出了什么问题。

I know there is work around but I'm more interested to know why I'm losing the session data.我知道有解决方法,但我更想知道为什么我会丢失 session 数据。 What's really going on behind the scene?幕后到底发生了什么? What can I do to solve this problem?我该怎么做才能解决这个问题? If you can elaborate it would really help.如果你能详细说明,那真的很有帮助。

Update更新

I've just tested my solution in Firefox (version 76.x) and my solution works.!我刚刚在 Firefox(版本 76.x)中测试了我的解决方案并且我的解决方案有效。! But it doesn't work on chrome (version 75.x)但它不适用于 chrome(版本 75.x)

You need to add this code to Global.asax Session_Start event您需要将此代码添加到 Global.asax Session_Start事件

if (Response.Cookies["ASP.NET_SessionId"] != null)
{
    Response.Cookies["ASP.NET_SessionId"].SameSite = SameSiteMode.None;
}

Please refer to this document for more details.有关详细信息,请参阅文档。

I don't know whether it's your problem or not but this year we've struggled from the same thing in our payment gateway and we realized that problem occurs from SameSite issue of Chrome .我不知道这是否是您的问题,但今年我们在支付网关中遇到了同样的问题,我们意识到问题来自Chrome 的 SameSite 问题 Adding some parameters to web.config fixed the issue for us.向 web.config 添加一些参数为我们解决了这个问题。

For .NET 4.7.2 and above use对于 .NET 4.7.2 及以上使用

<configuration>
 <system.web>  
  <sessionState cookieSameSite="None" /> 
 <system.web>
<configuration>

For older versions:对于旧版本:

<system.webServer>
   <rewrite>
      <outboundRules>
        <rule name="Add SameSite" preCondition="No SameSite">
          <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
          <action type="Rewrite" value="{R:0}; SameSite=None" />
          <conditions>
          </conditions>
        </rule>
        <preConditions>
          <preCondition name="No SameSite">
            <add input="{RESPONSE_Set_Cookie}" pattern="." />
            <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=None" negate="true" />
            <add input="{HTTP_USER_AGENT}" pattern="\(iP.*; CPU .*OS 12" negate="true" />
            <add input="{HTTP_USER_AGENT}" pattern="Macintosh; Intel Mac OS X 10_14.*Safari" negate="true" /> 
          </preCondition>
        </preConditions>
      </outboundRules>
   </rewrite>
</system.webServer>

This is how I did it after spending an entire day behind it and going through lots of articles at SO and elsewhere:在花了一整天时间并在 SO 和其他地方阅读了大量文章之后,我就是这样做的:

Step 1: Changed the Target Framework to 4.7.2 (it was 4.5.2 earlier)第 1 步:将目标框架更改为 4.7.2(之前是 4.5.2)

Step 2: Added the below 2 lines to the system.web section in the web.config file:第 2 步:将以下两行添加到 web.config 文件中的 system.web 部分:

    <sessionState cookieSameSite="None" />
    <httpCookies httpOnlyCookies="true" requireSSL="true"/>

That's all it took to get it going.这就是让它继续下去所需要的一切。 Now it works both in Chrome and Firefox (yet to be tested on another browsers, but hopefully, it will work on other browsers too).现在它可以在 Chrome 和 Firefox 中运行(尚未在其他浏览器上进行测试,但希望它也可以在其他浏览器上运行)。

PS:附言:

  • I know I have made a compromise in overall security by setting cookieSameSite to "None", but will definitely take steps to address that next.我知道我通过将 cookieSameSite 设置为“无”而在整体安全性方面做出了妥协,但接下来肯定会采取措施解决这个问题。

  • I was so much drowned in the problem, and then in the resolution that I felt so happy after getting through that it made me to write my first ever answer at SO.我被这个问题淹没了,然后在解决之后我感到非常高兴,这让我在 SO 写下了我的第一个答案。 So, please be gentle while commenting on the same if you do.所以,如果你这样做,请在评论时保持温和。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM