在ASP.NET Core 2.1中,请求不包含客户端的cookies的问题可能是由于未正确定义cookie选项或中间件配置不正确引起的。以下是解决此问题的代码示例:
Startup.cs
文件中添加services.AddMvc()
:public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
Configure()
方法中使用app.UseCookiePolicy()
来启用cookie策略中间件:public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 其他中间件配置...
app.UseCookiePolicy();
app.UseMvc();
}
ConfigureServices()
方法中添加services.AddAuthentication()
:public void ConfigureServices(IServiceCollection services)
{
// 其他服务配置...
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie();
}
Authorize
特性,确保在ConfigureServices()
方法中添加身份验证服务:public void ConfigureServices(IServiceCollection services)
{
// 其他服务配置...
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.Name = "YourCookieName";
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Strict;
});
}
这些步骤应该能够解决请求不包含客户端的cookies的问题。请根据您的具体情况进行调整。