简体   繁体   English

ASP.NET 核心标识未在浏览器中保存 cookie

[英]ASP.NET Core Identity not saving cookie in browser

I have search for a solution now for hours now...so here we go.我现在已经搜索了几个小时的解决方案......所以我们在这里 go。 I have 3 application: 1: API - Here we have the Authentication 2: Admin Client - Works great 2: User Client - Able to login and get Authenticated...but on every request I get the login screen again just as I am not authenticated (I get authenticated when I login).我有 3 个应用程序: 1:API - 在这里我们有身份验证 2:管理员客户端 - 效果很好 2:用户客户端 - 能够登录并获得身份验证......但是在每次请求时,我都会再次获得登录屏幕,就像我没有已通过身份验证(我登录时已通过身份验证)。 This application dos NOT save a cookie to the browser (admin does)此应用程序不会将 cookie 保存到浏览器(管理员会)

The client code in Program.cs (this is .net6) are the same in Admin and User apps. Program.cs 中的客户端代码(这是 .net6)在 Admin 和 User 应用程序中是相同的。 Only difference is the cookie name唯一的区别是 cookie 名称

    var _authenticationSettings = _serviceProvider.GetService<IAuthenticationSettings>();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                      .AddCookie(JwtBearerDefaults.AuthenticationScheme,
                        options => {
                            options.LoginPath = _authenticationSettings.LoginPath;
                            options.AccessDeniedPath = _authenticationSettings.AccessDeniedPath;
                            options.Events = new CookieAuthenticationEvents
                            {
                                // Check if JWT needs refreshment 
                                OnValidatePrincipal = RefreshTokenMonitor.ValidateAsync
                            };
                            options.Cookie.Name = "MainAppCookie";
                        }
                      );

I have used this method in other project and it worked just as it should....the only difference is that it was a .net5 IIS projects.....now I have .net6 running on Kestrel我在其他项目中使用过这种方法,它的工作原理应该是……唯一的区别是它是一个 .net5 IIS 项目……现在我在 Kestrel 上运行了 .net6

在此处输入图像描述

Any idea what I am doing wrong here...知道我在这里做错了什么......

The cookie gets created: cookie被创建:

在此处输入图像描述

with this value (not sure where I can find the values inside the cookie..使用这个值(不知道在哪里可以找到 cookie 中的值..

在此处输入图像描述

The cookie is valid for 30 min even when I have set the ExpireTimeSpan to 60 min即使我将 ExpireTimeSpan 设置为 60 分钟,cookie 的有效期也是 30 分钟

在此处输入图像描述

在此处输入图像描述

Here youy have the cookie status when I have logged in to MainApp client.当我登录到 MainApp 客户端时,这里有 cookie 状态。 Above you can see the AdminApp cookie (logged in there earlier)上面你可以看到 AdminApp cookie(之前在那里登录过)

在此处输入图像描述

And here you have the AdminApp cookie status on Login as well..在这里,您在登录时也有 AdminApp cookie 状态..

在此处输入图像描述

I must admit... this is really strange我必须承认……这真的很奇怪

Ok, after many hours on troubleshooting the issue I finally found the error.好的,经过数小时的故障排除后,我终于找到了错误。 There was a error in link... is was not calling a URL, is was calling a method:(链接中出现错误...不是在调用 URL,而是在调用方法:(

<a href="@_manager.LogOut()" class="menu-link px-5">@_localizer["SIGN_OUT"]</a>

should be:应该:

<a href="@Url.Action("LogOut", "Account")" class="menu-link px-5">@_localizer["SIGN_OUT"]</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM