You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在ASP.NET MVC 5中配置CHIPS Cookie:咨询微软相关技术指引

CHIPS Partitioned属性在ASP.NET中的配置问题

CHIPS的当前状态

CHIPS已经脱离实验阶段——Chrome在2024年1月启动的第三方Cookie拦截计划里,已将其作为官方支持的替代方案,属于稳定可用的标准。

ASP.NET的官方支持情况

目前ASP.NET确实没有提供通过web.config直接配置Partitioned属性的方式,也没有公开的官方指引。这和CHIPS是否处于实验阶段无关,主要是因为ASP.NET的官方更新节奏尚未跟上该标准的落地,暂时未将这个属性纳入配置化支持范围。

无需直接操作HttpCookie的替代方案

如果不想在代码里逐个处理HttpCookie对象,可以通过自定义模块或中间件,统一为符合条件的Cookie添加Partitioned属性:

ASP.NET Framework(自定义IHttpModule)

public class PartitionedCookieModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += OnEndRequest;
    }

    private void OnEndRequest(object sender, EventArgs e)
    {
        var response = ((HttpApplication)sender).Response;
        var setCookieHeaders = response.Headers.GetValues("Set-Cookie");
        if (setCookieHeaders == null) return;

        response.Headers.Remove("Set-Cookie");
        foreach (var header in setCookieHeaders)
        {
            // 仅为Secure且SameSite=None的Cookie添加Partitioned属性
            if (header.Contains("Secure") && header.Contains("SameSite=None"))
            {
                response.Headers.Add("Set-Cookie", $"{header}; Partitioned");
            }
            else
            {
                response.Headers.Add("Set-Cookie", header);
            }
        }
    }

    public void Dispose() { }
}

在web.config中注册该模块:

<system.webServer>
  <modules>
    <add name="PartitionedCookieModule" type="YourNamespace.PartitionedCookieModule" />
  </modules>
</system.webServer>

ASP.NET Core(中间件)

app.Use(async (context, next) =>
{
    await next();

    var setCookieHeaders = context.Response.Headers.GetCommaSeparatedValues("Set-Cookie");
    if (setCookieHeaders == null) return;

    context.Response.Headers.Remove("Set-Cookie");
    foreach (var header in setCookieHeaders)
    {
        if (header.Contains("Secure") && header.Contains("SameSite=None"))
        {
            context.Response.Headers.Append("Set-Cookie", $"{header}; Partitioned");
        }
        else
        {
            context.Response.Headers.Append("Set-Cookie", header);
        }
    }
});

内容的提问来源于stack exchange,提问作者K4M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:42:47