可以使用基于 Cookie 的会话共享。
protected void Application_Start(object sender, EventArgs e)
{
// 设置 Cookie 名称
SessionStateConfig.CookieName = ".AspNetCore.Session";
// 设置过期时间
SessionStateConfig.CookieExpiration = TimeSpan.FromDays(1);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// 启用 Cookie
services.Configure(options => {
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// 启用 Cookie
app.UseCookiePolicy();
app.UseSession();
app.UseMvc();
}
public HomeController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public IActionResult Index()
{
var cookieValue = _httpContextAccessor.HttpContext.Request.Cookies[".AspNetCore.Session"];
if (!string.IsNullOrEmpty(cookieValue))
{
HttpContext.Session.LoadFromBase64(cookieValue);
}
return View();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var sessionId = Request.Cookies[".AspNetCore.Session"].Value;
using (var httpClient = new HttpClient())
{
var response = httpClient.GetAsync($"https://{Constants.CoreSiteUrl}/api/session/{sessionId}").Result;
var session