Ich verwende Redis-Cache zum Speichern von ein paar Sachen in meinem Projekt.Speichern Sie Benutzersitzung in Redis mit ASP.NET Core in Azure
Ich verwende Azure (WebApp), und wenn ich einen SWAP zwischen meiner Vorproduktionsumgebung zur Produktion mache, ist die Benutzersitzung verloren und er muss sich erneut auf meiner Webseite anmelden.
Ich benutze Identität 3.0, mit UseCookieAuthentication. Ich möchte die "Sitzung" in Redis speichern, um mein Problem zu lösen, wenn ich den Swap mache.
Ich habe keine Informationen darüber gefunden, irgendwelche Ideen? Dank
Startup.cs-Code ConfigureServices:
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"));
}
Startup.cs-Code
// 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" });
});
}
Verwenden Sie den Azure-Redis-Cache? – juvchan
Ja, ich benutze redis Cache – chemitaxis