【App Service】应用在Entra ID认证完成后,回跳页面遇401错误 "Cookie is missing after a redirect from IDP, authentication has failed"

问题描述

Azure App Service上的应用,在完成用户登录认证后,需要回调到回跳页面(Callback) 时候遇见401报错,但是错误的提示消息很有意思,说是Cookie确实导致了认证失败。

Cookie is missing after a redirect from IDP, authentication has failed

 

问题解答

在问题出现后,通过浏览器的开发者模式,抓取了请求的网络日志。发现在callback请求之前的Authorize是正常完成的。

image

但是在Authorize请求之前的一个请求体中,发现无法设置Cookie的提示:

This attempt to set a cookie via a Set-Cookie header was blocked beacuse its Domain attribute was invalid with regards to the current host url

image

这个错误说明:

  • 浏览器在处理服务器返回的 Set-Cookie 响应头时,拒绝设置该 Cookie,原因是 Domain 属性不合法

设置Cookie的时候,Domain值得注意事项有:

 

  • 不要包含协议(如 http:// 或 https://)
  • 可以设置为主域名(如 example.com),以便子域共享 Cookie
  • 不能跨域设置(如从 a.com 设置 b.com 的 Cookie 会被浏览器拒绝)

 

而本次问题,恰好是Domain 值中包含了HTTPS协议, 所以无法在浏览器上设置Cookie,导致后续的请求因缺失必须的Cookie内容而失败。 当修改了Entra ID中的配置后,问题得到解决。

 

参考资料

cookie中的domain和path : https://www.cnblogs.com/zh1164/p/6529871.html

 
 
 

 

posted @ 2025-11-10 20:20  路边两盏灯  阅读(10)  评论(0)    收藏  举报