V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pedward
V2EX  ›  .NET

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

  •  
  •   pedward · 2021-11-22 15:42:08 +08:00 · 1751 次点击
    这是一个创建于 885 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    所以想解决的话,首先要搞清楚你的客户端行为,针对性的操作,最简单的就是:自定义重定向行为,客户端监测到重定向之后,拿到目标链接重新拼装请求。
    pedward
        2
    pedward  
    OP
       2021-11-23 10:21:29 +08:00
    @Vegetable 感谢回复,确实是出于安全性考虑,将 authorization 除去了。自己也在 stackoverflow 上找了找,也是这样说的( https://stackoverflow.com/questions/28564961/authorization-header-is-lost-on-redirect),看来是需要重新拼装请求。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3462 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:32 · PVG 19:32 · LAX 04:32 · JFK 07:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.