简体   繁体   English

在 ASP.NET MVC 中重定向到第 3 方 URL 后如何维护会话?

[英]How to maintain session after redirection to 3rd party URL in ASP.NET MVC?

Before the actual redirection to 3rd party url we are basically storing the user's session id, userid etc in Session so while we return back to our return url for posting data of payment gateway then we will need to update the status by retrieving the user's session, here at this point our session gets cleared/null.在实际重定向到第 3 方 url 之前,我们基本上将用户的会话 id、userid 等存储在 Session 中,因此当我们返回返回 url 以发布支付网关的数据时,我们将需要通过检索用户的会话来更新状态,在这一点上,我们的会话被清除/为空。 This is happening in Chrome for version above 84. Because I am able to maintain users session after redirection also in Firefox browser.这发生在 84 以上版本的 Chrome 中。因为我也能够在重定向后在 Firefox 浏览器中维护用户会话。 I also want to do the same irrespective of chrome versions.无论 chrome 版本如何,我也想做同样的事情。 How do I maintain session of each user after redirection?重定向后如何维护每个用户的会话?

Notes:笔记:

  1. I have tried dictionary by declaring it globally and filling values in it before redirect.我已经尝试通过全局声明字典并在重定向之前在其中填充值来尝试字典。 But that also get error that key not found after post bck to return url.但这也会导致在 post bck 返回 url 后找不到密钥的错误。
  2. I have tried session by storing user's info against the txnid that I send to 3rd party url and it sends us back to return url and that gets null.我已经尝试通过将用户的信息与我发送到第 3 方 url 的 txnid 相比较来尝试会话,它将我们发送回返回 url 并且结果为空。
  3. Cannot store session for each user in database as it will created prob and have load to db.无法在数据库中为每个用户存储会话,因为它会创建 prob 并加载到 db。

Any other options to try.任何其他选项都可以尝试。 Please help.请帮忙。 I am using ASP.NET MVC.我正在使用 ASP.NET MVC。

You need to set a SameSite cookie policy . 需要设置SameSite cookie 策略

Chrome enforces the new SameSite cookie rules more stringently than Firefox, but Firefox will soon behave the same as Chrome . Chrome 比 Firefox 更严格地执行新的SameSite cookie 规则,但 Firefox 很快将与 Chrome 相同

The exact SameSite option you should use depends on the type of redirection:您应该使用的确切SameSite选项取决于重定向的类型:

  • If the redirection is a HTTP 3xx redirection using GET or POST and all requests are over HTTPS then you can use SameSite=None .如果重定向是使用GETPOST的 HTTP 3xx 重定向,并且所有请求都通过 HTTPS,那么您可以使用SameSite=None
  • If the "redirection" is initiated by a client-side <meta> element or JavaScript using window.location = 'newUrl' then you can use SameSite=None .如果“重定向”是由客户端<meta>元素或 JavaScript 使用window.location = 'newUrl'那么您可以使用SameSite=None
  • If the redirection is a HTTP 3xx redirection using GET and not all requests are over HTTPS then you can use SameSite=Lax .如果重定向是使用GET的 HTTP 3xx 重定向,并且并非所有请求都通过 HTTPS,那么您可以使用SameSite=Lax
  • If the redirection is a HTTP 3xx redirection using POST and not all requests are over HTTPS then there is no quick-fix: you will need to use HTTPS for all requests.如果重定向是使用POST的 HTTP 3xx 重定向,并且并非所有请求都通过 HTTPS,则没有快速修复:您需要对所有请求使用 HTTPS。

The SameSite option was added to ASP.NET WebForms and ASP.NET MVC in .NET Framework 4.7.2, though you should be using .NET Framework 4.8. SameSite选项已添加到 .NET Framework 4.7.2 中的 ASP.NET WebForms 和 ASP.NET MVC,但您应该使用 .NET Framework 4.8。 If you cannot update your project to .NET Framework 4.7.2 or later then you can use a trick with IIS' <rewrite> rules to modify response Set-Cookie requests .如果您无法将项目更新到 .NET Framework 4.7.2 或更高版本,那么您可以使用带有 IIS 的<rewrite>规则的技巧来修改响应Set-Cookie requests


You can configure defaults for SameSite in your web.config file , but you'll need to update a couple of different locations: 您可以在 web.config 文件中为SameSite配置默认值,但您需要更新几个不同的位置:

  • Specify <system.web><httpCookies sameSite="Strict|Lax|None|Unspecified" /> for when you use HttpCookie directly.直接使用HttpCookie时指定<system.web><httpCookies sameSite="Strict|Lax|None|Unspecified" />
  • Specify <system.web><sessionState cookieSameSite="Strict|Lax|None" /> for ASP.NET's own Session cookie ( this is the OP's problem ).为 ASP.NET 自己的会话 cookie 指定<system.web><sessionState cookieSameSite="Strict|Lax|None" />这是 OP 的问题)。
    • Note that you cannot specify Unspecified for <sessionState> - but you should never use Unspecified so that shouldn't be an issue ( Unspecified is only needed if you need to support iOS 12 because Apple Safari did not recognize SameSite=None at the time, but no-one should be using iOS 12 today).请注意,您不能为<sessionState>指定Unspecified - 但您永远不应该使用Unspecified这样就不应该成为问题(仅当您需要支持 iOS 12时才需要Unspecified因为 Apple Safari 当时无法识别SameSite=None ,但今天没有人应该使用 iOS 12)。

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

相关问题 ASP.NET MVC 5:如何从 3rd 方库中禁用自定义错误页面 - ASP.NET MVC 5: How to disable custom error page from 3rd party library 从ASP.Net MVC控制器中的第三方API返回JSON字符串 - Returning JSON string from 3rd party API in ASP.Net MVC controller 在ASP.NET MVC中管理对第三方应用程序的共享API调用的最佳场所 - Best Place to manage shared API calls to 3rd party application inside my asp.net mvc asp.net将第3方dll部署到bin文件夹 - asp.net deploy 3rd party dlls to bin folder ASP.NET MVC维护对象会话 - ASP.NET MVC maintain object Session 如何在第三方ASP.NET Web API客户端中使用Oauth生成的令牌? - How to use an Oauth Generated Token in a 3rd party asp.net web API client? 如何在asp.net core web api中实现JWT Refresh Tokens(没有第三方)? - How to implement JWT Refresh Tokens in asp.net core web api (no 3rd party)? ASP.NET 核心 Web API - 如何使用第 3 方 API 基于条件 - ASP.NET Core Web API - How to Consume 3rd party API Based on Condition 如何在不等待 ASP.NET API 中的响应的情况下处理多个 3rd 方 API - How to process multiple 3rd party APIs without waiting for their response in ASP.NET API 如何在ASP.NET MVC Core 2.0中维护会话值 - How to maintain session value in asp.net mvc core 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM