DotNet Core 身份 - 它不记得用户
DotNet Core identity - it doesn't remember the user
我希望我的 DotNet Core 站点在几天甚至几周内记住登录用户。
但事实并非如此。 20分钟。这就是全部。
我已经尝试了所有可能的选项。没用。
我的所有普通 DotNet 和 OWIN 站点都这样做。 DotNetCore - 不!请帮忙 !!!!!!
更新:我添加了登录代码(这是典型的)用户 class。
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 的生命周期,你的 custom UserStore 应该实现 IUserSecurityStampStore 接口,不是普通的 IUserStore!
IUserSecurityStampStore 要求另外两个方法:SetSecurityStampAsync 和 GetSecurityStampAsync。
这就是线索。
我希望我的 DotNet Core 站点在几天甚至几周内记住登录用户。 但事实并非如此。 20分钟。这就是全部。
我已经尝试了所有可能的选项。没用。
我的所有普通 DotNet 和 OWIN 站点都这样做。 DotNetCore - 不!请帮忙 !!!!!!
更新:我添加了登录代码(这是典型的)用户 class。
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 的生命周期,你的 custom UserStore 应该实现 IUserSecurityStampStore 接口,不是普通的 IUserStore!
IUserSecurityStampStore 要求另外两个方法:SetSecurityStampAsync 和 GetSecurityStampAsync。 这就是线索。