首页 > 解决方案 > .Net Core Cookie 身份验证 - 用户收到错误声明

问题描述

我对托管在 IIS 中的 .net 核心 mvc 站点有一个非常奇怪的问题。

当用户使用此代码登录时:

var claimsIdentity = new ClaimsIdentity(
                     new List<Claim>() { new Claim(ClaimTypes.Role, "Admin", null), new Claim("Name", userName, null) }, CookieAuthenticationDefaults.AuthenticationScheme);

                    var authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30),
                        IsPersistent = false,
                    };

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

第一个用户登录时会显示他们的姓名。但是,当另一个用户登录时,他们会显示第一个用户名而不是他们自己的用户名。

我尝试过尝试所有 cookie 选项,但都没有成功,但这是我当前的配置:

 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
         .AddCookie(options => {
             options.Cookie.HttpOnly = false;
             options.LoginPath = "/Account/Login";
             options.LogoutPath = "/Account/LogOff";
             options.Cookie.IsEssential = true;
             options.Cookie.MaxAge = new TimeSpan(0, 2, 0, 0, 0);
             options.Cookie.SameSite = SameSiteMode.Strict;
             options.Cookie.Name = "SITECODEHERE";
         });

任何帮助将不胜感激,因为我确信我在某个地方犯了一个愚蠢的错误。

标签: asp.net-mvcasp.net-core-2.0.net-core-authorizationcore-authentication

解决方案


经过大量调查,事实证明该问题是由我们的防火墙上的缓存引起的。

删除它解决了这个问题。


推荐阅读