sta*_*SFT 7 nginx redis asp.net-core-mvc
示例项目https://github.com/xingyu217/Redis-nginx-aspnetcore-session nginx.config:
upstream study_server{
server localhost:8011 weight=1;
server localhost:8013 weight=1;
#server localhost:8014 weight=1;
#server 172.17.16.147:8012 weight=2;
#server 172.17.16.147:8011 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://study_server/;
proxy_cookie_path ~*^/.* /;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Run Code Online (Sandbox Code Playgroud)
获取会话 ID:ViewBag.seId= HttpContext.Session.Id;
启动.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
//services.AddDistributedMemoryCache();
services.AddDistributedRedisCache(options =>
{
options.Configuration = Configuration.GetConnectionString("RedisConnection");
options.InstanceName = "master";
});
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(100);
//options.Cookie.HttpOnly = true;
});
//services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
//app.UseDeveloperExceptionPage();
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
访问 URL: localhost 并一次又一次地刷新,会话 id 将一直在变化。
我检查了 redis 服务器中的密钥,它总是在刷新页面时生成新密钥。(例如masterdef69307-fbd3-c6ed-91d2-009b2306f902)
如果我只使用服务器(本地主机:8011):
upstream study_server{
server localhost:8011 weight=1;
#server localhost:8013 weight=1;
#server localhost:8014 weight=1;
#server 172.17.16.147:8012 weight=2;
#server 172.17.16.147:8011 weight=2;
}
Run Code Online (Sandbox Code Playgroud)
会话 ID 不会更改。
任何人都知道它将不胜感激。
谢谢
通过使用解决了它
var redis = ConnectionMultiplexer.Connect("<REDIS URI>");
services.AddDataProtection().PersistKeysToRedis(redis, "DataProtection-Keys");
Run Code Online (Sandbox Code Playgroud)
文章:
https://onecodex.ch/blogsdotnetcoredistributed-redis-cache/
必要的包:https ://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.Redis/
归档时间: |
|
查看次数: |
1002 次 |
最近记录: |