如何为ASP.NET Core Identity cookie设置路径

编程入门 行业动态 更新时间:2024-10-11 03:17:43
本文介绍了如何为ASP.NET Core Identity cookie设置路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ASP.NET Core MVC应用程序,该应用程序还托管一个API.该网站使用身份默认设置,因此在您登录cookie时进行了设置.该API已配置为使用JWT承载身份验证.

I have an ASP.NET Core MVC application that also hosts an API. The site uses Identity defaults so when you log in a cookie is set. The API has been configured to use JWT Bearer authentication.

我还有一个单独的ASP.Net Core MVC应用程序,该应用程序承载一个Javascript SPA,该Java SPA作为身份验证服务器和API与该其他站点进行通信.

I also have a separate ASP.Net Core MVC app that hosts a Javascript SPA that communicates to this other site as the authentication server and the API.

在我的开发机上,这些项目使用不同的端口,并且当我登录到主API站点时,将设置cookie,并且可以看到cookie路径设置为根"/".这是所需的行为,因为我希望Javascript SPA知道我已通过身份验证并登录.

On my dev machine these projects use different ports and when the I log in to the main API site a cookie is set and I can see that the cookie path is set to root " / ". This is the desired behaviour since I want the Javascript SPA to know that I am authenticated and logged in.

当我将其部署到我们的IIS服务器时,我为每个应用程序提供了一条虚拟路径,如下所示:

When I deploy this to our IIS server, I give each app a virtual path like so:

网站1:/SPA网站2:/API

Site 1: /SPA Site 2: /API

将其部署到IIS时,我注意到cookie路径设置为"/API".

When I deploy this to IIS, I notice that the cookie Path is set to "/API".

这不是理想的行为,因为SPA应用程序认为我未通过身份验证.

This is not the desired behaviour as the SPA app thinks I am not authenticated.

我希望能够覆盖它,并将cookie路径明确设置为根"/".

I want to be able to override this and set the cookie path explicitly to root "/".

如何在仍然允许JWT Bearer身份验证的同时设置cookie路径?

How do I set the the cookie path while still allowing for JWT Bearer authentication?

推荐答案

您应该能够在 ConfigureServices 中配置cookie路径,例如:

You should be able to configure the cookie path in ConfigureServices like:

services.ConfigureApplicationCookie(options => { options.Cookie.Path = "/"; });

请参见 docs.microsoft/zh-cn/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-2.1&tabs=aspnetcore2x#cookie-settings

更多推荐

如何为ASP.NET Core Identity cookie设置路径

本文发布于:2023-11-17 09:07:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   路径   ASP   Core   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!