首页 > 解决方案 > 为什么 ASP.NET Core Identity Cookie 不能跨域工作?

问题描述

我在我的 ASP.NET Core Identity 中设置了一些 cookie 选项,包括几个子域:

        services.ConfigureApplicationCookie(identityOptionsCookies =>
        {
            identityOptionsCookies.Cookie.Domain = ".example.com";
            identityOptionsCookies.Cookie.Name = "ExampleToken";
        });

基于此配置,我通过此子域登录:

api.accounts.example.com

我使用此 API 调用检查我是否已通过身份验证:

api.accounts.example.com/identity/isAuthenticated => returns true

现在我转到另一个子域,对 Startup.cs 使用相同的配置,我不再经过身份验证:

api.user.example.com/teacher/list => redirects me to /Accounts/Login

为什么会这样?

标签: c#asp.net-corecookiesasp.net-core-identity

解决方案


identityOptionsCookies.Cookie.Domain = "*.example.com";


推荐阅读