c# - 如何在 asp.net core 中为 cookie 添加前缀?
问题描述
我在 SecurityHeaders.com 上运行了扫描,它显示 cookie 没有前缀并且我不知道如何向 cookie 添加前缀的警告。谁能告诉我如何在 asp.net core 中做到这一点?网站扫描结果截图
这是 Startup.cs 类中的 ConfigureServices 方法
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))
.AddCookie();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDistributedMemoryCache();
services.AddSession();
}
这是配置方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute("home", "{action=Index}",
defaults: new { controller = "Home" });
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
解决方案
我找到了答案。因此,如果有人需要,请在此处发布。在 SessionOptions 中,将 Cookie.Name 设置为 prefix+name。
__Secure-下面是会话 Cookie 名称中的前缀。
services.AddSession(options =>
{
options.Cookie.Name = "__Secure-.AspNetCore.Session";
//options.IdleTimeout = TimeSpan.FromSeconds(600);
//options.Cookie.IsEssential = true;
});
是的,它也解决了扫描中的安全标头问题。
推荐阅读
- c - 如何链接这两个功能
- cmd - 使用 cmd 从 HTTPS 服务器自动下载文件
- angular - 不能在变量中使用 Angular routerLink
- python-3.x - python argparse 在使用特定选项时忽略其他选项
- r - 新类的方法如何引用它要替换的基本功能?
- arrays - 带有数组的 Wordpress 查询
- javascript - 将字符串从一个视图传递到控制器 ASP.NET MVC5 的问题
- php - 使用 PHP 和 PDO 的搜索功能不起作用
- python - 替换熊猫数据框中的多个值名称
- java - 如何从java获取arraylist到android