ASP.NET CORE 身份验证 cookie 消失

时间:2021-01-19 15:12:04

标签: angular asp.net-identity asp.net-core-2.2

英语不是第一语言,所以如果听起来很奇怪,请告诉我:)

我的开发环境

Asp.net Core 2.2 && Angular 10.0

目前,我遇到这个问题已经 3 个月了,但仍然不知道什么是解决方案。 所以这次出现了问题

> Mysite.com ----> **Survey.com ----> Mysite.com**

当用户从“Survey.com”返回到“Mysite.com”时,随机身份验证 cookie 会消失。 下面是“Start.cs”的一部分。如果每个用户都丢失了他们的身份验证 cookie,那么我可以清楚地看到问题的原因,但即使是同一台 PC 也是随机发生的(但如果我能够重新登录,它不会发生一段时间并突然发生)。

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        //Add indentity types
        services.AddIdentity<TUserName, TTesterRole>(config =>
        {
            config.SignIn.RequireConfirmedEmail = true;
            config.Password.RequireDigit = true;
            config.Password.RequiredLength = 8;
            config.Password.RequireNonAlphanumeric = true;
            config.Password.RequireUppercase = true;
            config.Password.RequireLowercase = true;
            config.Password.RequiredUniqueChars = 1;
            config.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.,/;'`!@#$%^&*()_+[]{}";
        })
        .AddDefaultTokenProviders()
        .AddTokenProvider<DataProtectorTokenProvider<TUserName>>("Default");

        // Identity Services
        services.AddTransient<IUserStore<TUserName>, PKRctUserDatastore>();
        services.AddTransient<IRoleStore<TTesterRole>, PKRctRoleDatastore>();
        services.AddTransient<PKRctDataTable>();

        // linq error logs
        services.AddLogging(loggingBuilder => {
            loggingBuilder.AddConsole()
                .AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Information);
            loggingBuilder.AddDebug();
        });

        // protected token life
        services.Configure<DataProtectionTokenProviderOptions>(options =>
        {
            options.TokenLifespan = TimeSpan.FromDays(3);
        });

        // Add application services
        services.AddTransient<IEmailSender, MessageSender>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSignalR();

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });

        //// set cookies - Identity cookie setting
        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = true;
            options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
            options.Cookie.SameSite = SameSiteMode.Lax;
            options.Cookie.Name = "MYIDENTITY";
            options.Cookie.IsEssential = true;
            options.LoginPath = "/Account/Login";
            options.ExpireTimeSpan = TimeSpan.FromDays(3);
            options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
            options.SlidingExpiration = true;
        });

请帮帮我!

0 个答案:

没有答案