.net core web api 添加对session跨域实现

1.配置Startup

/ConfigureServices添加:

services.AddSession(options =>
            {
                options.Cookie.Name = ".AdventureWorks.Session";
                options.IdleTimeout = System.TimeSpan.FromSeconds(120);//设置session的过期时间
                options.Cookie.HttpOnly = true;//设置在浏览器不能通过js获得该cookie的值
            });
            services.TryAddSingleton();
            services.AddHttpContextAccessor();
            #region 跨域
            services.AddCors(options =>
            options.AddPolicy("AllowSameDomain",
            builder => builder.WithOrigins().AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().AllowCredentials()));
            #endregion
//Configure添加:
app.UseCookiePolicy();
app.UseSession();

2.控制器启用

[EnableCors("AllowSameDomain")]

3.Ajax异步跨域调用

"http://www.w3.org/1999/xhtml">

"Content-Type" content="text/html; charset=utf-8"/>
    
    
    
    



 

 

 

 

你可能感兴趣的:(.net core web api 添加对session跨域实现)