.net核心应用程序SignInAsync来自其他域失败

时间:2019-06-10 18:13:18

标签: .net authentication asp.net-core-2.2

将条目链接粘贴到浏览器中时,身份验证可以正常工作。但是,当从其他域重定向时,它会失败(调用SignInAsync后,HttpContext.User.Identity.IsAuthenticated为false)。

正在调用的应用程序具有

            app.Use(async (context, next) =>
            {
                context.Response.Headers.Add("Referrer-Policy", "no-referrer");
                await next();
            });

并且确实没有在标头中发送HTTP引用。

那是怎么回事?

这是一个.net core 2.2应用程序,我的服务配置是:

            services.Configure<CookiePolicyOptions>(options =>
            {
                options.MinimumSameSitePolicy = SameSiteMode.Lax;
            });
            services.AddMvc()
                    .AddRazorPagesOptions(options =>
                    {
                        options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());//turns off AntiForgery checking
                    })
                    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.Cookie.SameSite = SameSiteMode.Lax;
                options.AccessDeniedPath = "/AccessDenied";
            });

和身份验证:

            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, NameString)
            };

            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme);

            AuthenticationProperties properties = new AuthenticationProperties
            {
                IsPersistent = false
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                properties);

1 个答案:

答案 0 :(得分:0)

该解决方案就像在SametimeCookiePolicyOptions和AddCookie调用上都使用SameSiteMode一样简单。