C# 使用Azure中的ASP.NET核心在Redis中保存用户会话
我正在使用redis缓存在我的项目中保存一些东西 我正在使用Azure(WebApp),当我在试生产环境与生产环境之间进行交换时,用户会话将丢失,他需要重新登录我的网页 我正在使用Identity 3.0和UseCookie身份验证。我想将“会话”存储在Redis中,以便在交换时解决我的问题 我没有找到关于它的信息,有什么想法吗?谢谢 Startup.cs代码配置服务:C# 使用Azure中的ASP.NET核心在Redis中保存用户会话,c#,azure,redis,asp.net-core,asp.net-identity-3,C#,Azure,Redis,Asp.net Core,Asp.net Identity 3,我正在使用redis缓存在我的项目中保存一些东西 我正在使用Azure(WebApp),当我在试生产环境与生产环境之间进行交换时,用户会话将丢失,他需要重新登录我的网页 我正在使用Identity 3.0和UseCookie身份验证。我想将“会话”存储在Redis中,以便在交换时解决我的问题 我没有找到关于它的信息,有什么想法吗?谢谢 Startup.cs代码配置服务: public void ConfigureServices(IServiceCollection services)
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
// Registers MongoDB conventions for ignoring default and blank fields
// NOTE: if you have registered default conventions elsewhere, probably don't need to do this
//RegisterClassMap<ApplicationUser, IdentityRole, ObjectId>.Init();
AutoMapperWebConfiguration.Configure();
services.AddSingleton<ApplicationDbContext>();
// Add Mongo Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>(o =>
{
// configure identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonLetterOrDigit = false;
o.Password.RequiredLength = 6;
o.User.RequireUniqueEmail = true;
o.Cookies.ApplicationCookie.CookieSecure = CookieSecureOption.SameAsRequest;
o.Cookies.ApplicationCookie.CookieName = "MyCookie";
})
.AddMongoStores<ApplicationDbContext, ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(60);
options.CookieName = "MyCookie";
});
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddLocalization(options => options.ResourcesPath = "Resources");
// Caching This will add the Redis implementation of IDistributedCache
services.AddRedisCache();
services.Configure<RedisCacheOptions>(options =>
{
options.Configuration = Configuration["RedisConnection"];
});
services.AddCaching();
// Add MVC services to the services container.
services.AddMvc(options =>
{
options.CacheProfiles.Add("OneDay",
new CacheProfile()
{
Duration = 86400,
Location = ResponseCacheLocation.Any
});
options.CacheProfiles.Add("OneMinute",
new CacheProfile()
{
Duration = 60,
Location = ResponseCacheLocation.Any
});
})
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.Configure<AppOptions>(Configuration.GetSection("AppOptions"));
}
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddApplicationInsightsTelemetry(配置);
//注册MongoDB约定以忽略默认字段和空白字段
//注意:如果您已经在其他地方注册了默认约定,则可能不需要这样做
//RegisterClassMap.Init();
AutoMapperWebConfiguration.Configure();
services.AddSingleton();
//将Mongo标识服务添加到服务容器中。
服务。附加性(o=>
{
//配置标识选项
o、 Password.RequireDigit=false;
o、 Password.RequireLowercase=false;
o、 Password.RequireUppercase=false;
o、 Password.requireNonletterDigit=false;
o、 Password.RequiredLength=6;
o、 User.RequireUniqueEmail=true;
o、 Cookies.applicationcokie.CookieSecure=CookieSecureOption.SameAsRequest;
o、 Cookies.applicationcokie.CookieName=“mycokie”;
})
.AddMongoStores()
.AddDefaultTokenProviders();
services.AddSession(选项=>
{
options.IdleTimeout=TimeSpan.frommins(60);
options.CookieName=“MyCookie”;
});
services.Configure(Configuration.GetSection(“AppSettings”);
services.AddLocalization(options=>options.ResourcesPath=“Resources”);
//缓存这将添加IDistributedCache的Redis实现
services.AddRedisCache();
配置(选项=>
{
options.Configuration=Configuration[“重新连接”];
});
services.AddCaching();
//将MVC服务添加到服务容器中。
services.AddMvc(选项=>
{
options.CacheProfiles.Add(“一天”,
新缓存配置文件()
{
持续时间=86400,
位置=响应确认位置。任何
});
options.CacheProfiles.Add(“一分钟”,
新缓存配置文件()
{
持续时间=60,
位置=响应确认位置。任何
});
})
.AddViewLocalization(选项=>options.ResourcesPath=“资源”)
.AddDataAnnotationsLocalization();
services.Configure(Configuration.GetSection(“AppOptions”);
}
Startup.cs代码
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseSession();
app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseIdentity();
app.UseCookieAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.LoginPath = new PathString("/Account/Login");
options.AutomaticChallenge = true;
});
var requestLocalizationOptions = new RequestLocalizationOptions
{
// Set options here to change middleware behavior
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")
},
RequestCultureProviders = new List<IRequestCultureProvider>
{
new CookieRequestCultureProvider
{
CookieName = "_cultureLocalization"
},
new QueryStringRequestCultureProvider(),
new AcceptLanguageHeaderRequestCultureProvider
{
}
}
};
app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture: new RequestCulture("en-US"));
app.UseFacebookAuthentication(options =>
{
options.AppId = "*****";
options.AppSecret = "****";
});
app.UseGoogleAuthentication(options =>
{
options.ClientId = "*****";
options.ClientSecret = "***";
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "view",
template: "{customName}/{id}",
defaults: new { controller = "View", action = "Index" });
});
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
//
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseSession();
app.UseIISPlatformHandler(options=>options.AuthenticationDescriptions.Clear());
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseIdentity();
app.UseCookieAuthentication(选项=>
{
options.AutomaticAuthenticate=true;
options.LoginPath=新路径字符串(“/Account/Login”);
options.AutomaticChallenge=true;
});
var requestLocalizationOptions=新的requestLocalizationOptions
{
//在此处设置选项以更改中间件行为
SupportedCultures=新列表
{
新文化信息(“美国”),
新文化信息(“es”)
},
SupportedCultures=新列表
{
新文化信息(“美国”),
新文化信息(“es”)
},
RequestCultureProviders=新列表
{
新CookiierRequestCultureProvider
{
CookieName=“\u文化本地化”
},
新建QueryStringRequestCultureProvider(),
新的AcceptLanguageHeaderRequestCultureProvider
{
}
}
};
app.UseRequestLocalization(requestLocalizationOptions,defaultRequestCulture:newrequestCulture(“en-US”);
app.UseFacebookAuthentication(选项=>
{
options.AppId=“*******”;
options.AppSecret=“****”;
});
app.UseGoogleAuthentication(选项=>
{
options.ClientId=“*******”;
options.ClientSecret=“***”;
});
app.UseMvc(路由=>
{