我在 dot net core3.1 web api 中从 ajax 调用 post 方法。 它不工作 我在启动页面添加了这个 services.AddCors(c => { c.AddPolicy("AllowOrigin", options => options.AllowAny ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
以下是我来自startup.cs的代码,
public void ConfigureServices(IServiceCollection services){
services.AddMvc().AddJsonOptions(options =>{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddDistributedMemoryCache();
services.AddSession(options => {
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(36000);
options.Cookie.HttpOnly = true;
});
services.AddCors(options => {
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env{
if (env.IsDevelopment()){
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
//app.UseCors("CorsPolicy");
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseSession();
app.UseAuthentication();
app.UseMvc();
}
以下是在会话中设置和获取用户ID的代码:
HttpContext.Session.SetInt32("UserId", user.Id);
int userId = (int)HttpContext.Session.GetInt32("UserId");
在从与设置它的操作不同的操作中读取会话时,我遇到异常。 会话无法正常工作的任何原因?
此解决方案仅适用于dotnetcore 2.1或更高版本。 我的解决方案是dotnetcore 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.