首页 > 解决方案 > 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; }
    ....
}

标签: asp.net-core-2.1asp.net-core-identity

解决方案


我找到了答案。

为了控制授权 cookie 的生命周期,您的自定义 UserStore应该实现IUserSecurityStampStore接口,而不是普通的IUserStore

IUserSecurityStampStore要求另外两种方法:SetSecurityStampAsyncGetSecurityStampAsync。这就是线索。


推荐阅读