ASP.NET Core 3.1 Razor Pages SameSite cookie 示例ASP.NET Core 3.1 Razor Pages SameSite cookie sample

本文内容

ASP.NET Core 3.0 内置了对SameSite属性的支持,包括 SameSiteMode 属性值 Unspecified 以禁止写入特性。

编写 SameSite 属性Writing the SameSite attribute

下面是如何在 cookie 上编写 SameSite 属性的示例;

  1. var cookieOptions = new CookieOptions
  2. {
  3. // Set the secure flag, which Chrome's changes will require for SameSite none.
  4. // Note this will also require you to be running on HTTPS
  5. Secure = true,
  6. // Set the cookie to HTTP only which is good practice unless you really do need
  7. // to access it client side in scripts.
  8. HttpOnly = true,
  9. // Add the SameSite attribute, this will emit the attribute with a value of none.
  10. // To not emit the attribute at all set the SameSite property to SameSiteMode.Unspecified.
  11. SameSite = SameSiteMode.None
  12. };
  13. // Add the cookie to the response cookie collection
  14. Response.Cookies.Append(CookieName, "cookieValue", cookieOptions);

设置 Cookie 身份验证和会话状态 cookieSetting Cookie Authentication and Session State cookies

Cookie 身份验证、会话状态和各种其他组件通过 Cookie 选项设置其 sameSite 选项,例如

  1. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  2. .AddCookie(options =>
  3. {
  4. options.Cookie.SameSite = SameSiteMode.None;
  5. options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
  6. options.Cookie.IsEssential = true;
  7. });
  8. services.AddSession(options =>
  9. {
  10. options.Cookie.SameSite = SameSiteMode.None;
  11. options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
  12. options.Cookie.IsEssential = true;
  13. });

在上面显示的代码中,cookie 身份验证和会话状态将其 sameSite 属性设置为 None,使用 None 值发出属性,同时将 Secure 特性设置为 true。

运行示例Run the sample

如果运行示例项目,请在初始页面上加载浏览器调试器,并使用它查看站点的 cookie 集合。若要在 Edge 和 Chrome 中进行此操作,请按 F12 选择 "Application" 选项卡,然后单击 "Storage" 部分中 "Cookies" 选项下的 "站点 URL"。

浏览器调试器 Cookie 列表

您可以从上图中看到,当您单击 "创建 SameSite Cookie" 按钮的 SameSite 属性值为 Lax,与在示例代码中设置的值匹配时,示例创建的 cookie 就会看到该示例创建的 cookie。

截获 cookieIntercepting cookies

为了截获 cookie,若要根据用户的浏览器代理中的支持来调整无值,必须使用 CookiePolicy 中间件。这必须放入 http 请求管道中,然后才能ConfigureServices()中写入 cookie 和配置的任何组件。

若要将其插入管道中,请使用Startup.csConfigure(IApplicationBuilder, IHostingEnvironment) 方法中的 app.UseCookiePolicy()例如:

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  2. {
  3. if (env.IsDevelopment())
  4. {
  5. app.UseDeveloperExceptionPage();
  6. }
  7. else
  8. {
  9. app.UseExceptionHandler("/Home/Error");
  10. app.UseHsts();
  11. }
  12. app.UseHttpsRedirection();
  13. app.UseStaticFiles();
  14. app.UseCookiePolicy();
  15. app.UseAuthentication();
  16. app.UseSession();
  17. app.UseMvc(routes =>
  18. {
  19. routes.MapRoute(
  20. name: "default",
  21. template: "{controller=Home}/{action=Index}/{id?}");
  22. });
  23. }

然后,在 ConfigureServices(IServiceCollection services) 配置 cookie 策略,以在附加或删除 cookie 时调用帮助器类,如下所示:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.Configure<CookiePolicyOptions>(options =>
  4. {
  5. options.CheckConsentNeeded = context => true;
  6. options.MinimumSameSitePolicy = SameSiteMode.None;
  7. options.OnAppendCookie = cookieContext =>
  8. CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
  9. options.OnDeleteCookie = cookieContext =>
  10. CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
  11. });
  12. }
  13. private void CheckSameSite(HttpContext httpContext, CookieOptions options)
  14. {
  15. if (options.SameSite == SameSiteMode.None)
  16. {
  17. var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
  18. if (SameSite.BrowserDetection.DisallowsSameSiteNone(userAgent))
  19. {
  20. options.SameSite = SameSiteMode.Unspecified;
  21. }
  22. }
  23. }

Helper 函数 CheckSameSite(HttpContext, CookieOptions)

  • 当 cookie 追加到请求或从请求中删除时调用。
  • 检查 SameSite 属性是否设置为 None
  • 如果 SameSite 设置为 None 并且已知当前用户代理不支持 none 特性值。使用SameSiteSupport类完成检查:
    • 通过将属性设置为,将 SameSite 设置为不发出该值 (SameSiteMode)(-1)

更多信息More Information

ASP.NET Core SameSite 文档Chrome 更新