商界观察员 發表於 2025-3-27 20:19:00

.net core cookie授权给非服务器域名的网站时可能无法正常删除的解决办法

<p>  <strong>背景介绍:.net core框架,API服务器域名是a.com, 服务器需要通过cookie授权给网站b.com域名并设置了授权域名为a.com。 当你想退出时,在网站b.com使用js清理了本域名下的所有cookie后,刷新浏览器cookie列表也确实看到本域名下没有cookie了。 这时再使用另一个账号登录后会发现还是原用户的登录信息。</strong></p>
<p>&nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; 原因分析:上面已经提到了,授权API服务器设置了授权cookie的域名为a.com,如果网站b.com仅清理本域名下的cookie自然无法删除a.com的授权cookie(浏览器不允许跨域操作或访问cookie)。</p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; 解决方案:&nbsp; 1.&nbsp; 调用跟授权cookie域名同源的API服务器的退出接口。&nbsp; &nbsp;如调用: a.com/logout。&nbsp; 通过同源域名下的服务器来删除授权cookie,问题正常解决。</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2. 设置每次调用登录接口时,不判断用户是否已授权,直接返回新的授权cookie。&nbsp; &nbsp; 通过强制覆盖客户端授权Cookie处理,问题正常解决。</strong></p>
<p>&nbsp;</p>
<p><strong>&nbsp;这里把cookie跨域授权的核心示例展示一下,供参考:</strong></p>
<div class="cnblogs_code">
<pre>services.AddAuthentication().AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =&gt;<span style="color: rgba(0, 0, 0, 1)">
{
    options.LoginPath </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/admin/login</span><span style="color: rgba(128, 0, 0, 1)">"</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">未登录重定向路径</span>
    options.LogoutPath = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/login/logout</span><span style="color: rgba(128, 0, 0, 1)">"</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">未授权时重定向路径</span>
    options.ExpireTimeSpan = TimeSpan.FromDays(<span style="color: rgba(128, 0, 128, 1)">1</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">授权有效期</span>
    options.Cookie.Domain = CONST.DEFAULT_DOMAIN;   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Cookie存储域名</span>
    options.Cookie.Name = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">AdminUserToken</span><span style="color: rgba(128, 0, 0, 1)">"</span>;      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Cookie名称</span>
    options.Cookie.Path = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    options.Cookie.IsEssential </span>= <span style="color: rgba(0, 0, 255, 1)">true</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否强制存储cookie,即使用户不同意使用cookie,也可以强制存储.</span>
    options.Cookie.HttpOnly = <span style="color: rgba(0, 0, 255, 1)">true</span>;    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置是否允许客户端JS可访问授权cookie:true=不允许</span>
    options.Cookie.SameSite = SameSiteMode.None;    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置跨域时设置为:None</span>
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always; <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)">/设置跨域时设置为:CookieSecurePolicy.Always:确保Cookie通过 HTTPS 发送</span>
    options.Events = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CookieAuthenticationEvents
    {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 重写 OnRedirectToLogin 事件,防止重定向,返回 401</span>
      OnRedirectToLogin = context =&gt;<span style="color: rgba(0, 0, 0, 1)">
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (context.Request.Path.StartsWithSegments(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/user</span><span style="color: rgba(128, 0, 0, 1)">"</span>) &amp;&amp; context.Response.StatusCode == <span style="color: rgba(128, 0, 128, 1)">200</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                context.Response.StatusCode </span>= <span style="color: rgba(128, 0, 128, 1)">401</span><span style="color: rgba(0, 0, 0, 1)">;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Task.CompletedTask;
      }
    };
});</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="background-color: #dee7de; font-size: 14px; line-height: 1.8; color: #333;">
      *感谢您的阅读。喜欢的、有用的就请大哥大嫂们高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。欢迎转载!
<br>
      *博主的文章是自己平时开发总结的经验,由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。
<br>
      *我的博客:
http://www.cnblogs.com/lxhbky/
</div><br><br>
来源:https://www.cnblogs.com/lxhbky/p/18796742
頁: [1]
查看完整版本: .net core cookie授权给非服务器域名的网站时可能无法正常删除的解决办法