pedward
V2EX  ›  .NET

.net6 使用 UseHttpsRedirection 中间件强制将请求从 http 转 https,但 Authorization 信息丢失

  •  
  •   pedward · Nov 22, 2021 · 2723 views
    This topic created in 1632 days ago, the information mentioned may be changed or developed.

    前端(前端是一个.netcore 2.1 的 mvc 项目)有一个带 Authorization 的 http 请求去 call api(api 是.net6 的),api 会使用 UseHttpsRedirection 中间件会将 http 转 https ,但转的过程中 Authorization 信息丢失了,导致返回 401 。两个项目都是在本地开发环境运行的。

    各位大佬们,知道什么原因吗?

    2 replies    2021-11-23 10:21:29 +08:00
    Vegetable
        1
    Vegetable  
       Nov 22, 2021
    部分客户端会在重定向时,有选择的移除 authorization 这个 header ,这是一种出于安全性考虑的进行的设计。
    比如 axios 使用的依赖 follow-redirects 这部分代码
    https://github.com/follow-redirects/follow-redirects/blob/main/index.js#L395

    所以想解决的话,首先要搞清楚你的客户端行为,针对性的操作,最简单的就是:自定义重定向行为,客户端监测到重定向之后,拿到目标链接重新拼装请求。
    pedward
        2
    pedward  
    OP
       Nov 23, 2021
    @Vegetable 感谢回复,确实是出于安全性考虑,将 authorization 除去了。自己也在 stackoverflow 上找了找,也是这样说的( https://stackoverflow.com/questions/28564961/authorization-header-is-lost-on-redirect),看来是需要重新拼装请求。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5104 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    ♥ Do have faith in what you're doing.