asp.net-core-2.1 - DotNet Core 身份 - 它不记得用户
问题描述
我希望我的 DotNet Core 站点能够记住几天甚至几周的登录用户。但事实并非如此。20分钟。这就是全部。
我已经尝试了所有可能的选择。不工作。
我所有的普通 DotNet 和 OWIN 站点都这样做。DotNetCore - 不!请帮忙 !!!!!!
更新:我添加了登录代码(这是典型的)一个用户类。
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<Core.Client.API.Models.User, Core.Client.API.Models.UserRole>().AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.AllowedForNewUsers = true;
});
services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.Name = "**********.Auth";
options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.LoginPath = "*******";
options.AccessDeniedPath = "**********";
options.SlidingExpiration = true;
});
services.AddAntiforgery();
services.AddMemoryCache();
services.AddTransient<IUserStore<Core.Client.API.Models.User>, Core.Logic.Services.UserService>();
services.AddTransient<IRoleStore<Core.Client.API.Models.UserRole>, Core.Logic.Services.UserRoleService>();
services.AddTransient<Core.Storage.Api.IUserStore, Core.Db.Services.UserStore>();
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
//SignIn Code
public async Task<ActionResult> Login()
{
await _signInManager.SignOutAsync();
var u = await _userManager.FindByNameAsync("UserName");
if(u != null)
{
await _signInManager.SignInAsync(u, true);
}
return new RedirectToActionResult(nameof(HomeController.Index), "Home", null);
}
//User class
public class User : IIdentity
{
public Guid Id { get; set; } = Guid.NewGuid();
public bool IsAuthenticated { get; set; }
public string AuthenticationType { get; set; }
public string Name { get; set; }
....
}
解决方案
我找到了答案。
为了控制授权 cookie 的生命周期,您的自定义 UserStore应该实现IUserSecurityStampStore接口,而不是普通的IUserStore!
IUserSecurityStampStore要求另外两种方法:SetSecurityStampAsync和GetSecurityStampAsync。这就是线索。
推荐阅读
- jquery - 第一封电子邮件后无法向 forumspree.io 提交表单
- html - 如何在图像顶部添加 h1,在中间添加段落并使图像变暗?
- opencl - OpenCL:来自不同程序的内核并行执行
- google-cloud-platform - GCP Cloudsql“按需”备份是增量的还是完整的?
- javascript - 如何在html中使用js变量?
- android - Android - this.cordova.getActivity().getApplicationContext() 的空指针异常
- loops - Tcl/tk:在条件语句中返回与中断?
- apache-flink - Apache Flink:如何使用 SourceFunction 以指定的时间间隔执行任务?
- intellij-idea - 向 IntelliJ 添加 Markdown JSX 语言注入
- kubernetes - kubespray 仪表板警告禁止弹出窗口