aspnetcore 3.1 mvc 身份验证 支付宝浏览器失效 无法登录

2020-10-10 10:22发布

问题:

关于支付宝浏览器身份验证失效,无法登录。
我设置了CheckConsentNeeded:false
并且身份验证 IsEssential :true
但都在支付宝浏览器无法登录,错误日志是 :AuthenticationScheme: Cookies was not authenticated.

@dudu

public void ConfigureServices (IServiceCollection services) {

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(option => {
            option.LoginPath = new PathString("/Visitor/login");
            option.AccessDeniedPath = new PathString("/Visitor/Logout");
            option.Cookie.Path = "/";
            option.Cookie.HttpOnly = true;
            option.Cookie.IsEssential = true;
        });

    }

public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {
        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        } else {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        LogManager.LoadConfiguration("nlog.config");

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseCookiePolicy();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints => {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=visitor}/{action=index}/{id?}");
        });

        app.UseWxPayment();
    }

回答1:

我们设置了

options.Cookie.SameSite = SameSiteMode.Unspecified;
options.Cookie.SecurePolicy = CookieSecurePolicy.None;

没有设置

options.CheckConsentNeeded = context => false;
option.Cookie.IsEssential = true;