首页 > 解决方案 > 在 ASP.NET 4.8 和 ASP.NET Core 3.1 之间共享身份验证 cookie

问题描述

我按照此Microsoft 文档中描述的步骤操作,但无法使共享身份验证正常工作。

我可以看到这两个 webapps 都生成了 auth cookie,但它们最终会相互覆盖,因此 auth 仅适用于您最后登录的任何应用程序。

我已经在 IIS10 中设置了两个 webapps。4.8 应用程序是一个网站,Core 3.1 应用程序是其中的一个应用程序,因此它们都在运行并且在同一个应用程序池中。我将两个 cookie 上的路径都设置为“/”,因为核心 webapp 在 localhost/core 上运行。

这是我在 ASP.NET 4.8 Web Forms webapp 上的启动

    public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\test\core");
        //app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            AuthenticationMode =
                Microsoft.Owin.Security.AuthenticationMode.Active,
            LoginPath = new PathString("/Account/Login.aspx"),
            CookieName = ".AspNet.SharedCookie",
            CookiePath = "/",
            TicketDataFormat = new AspNetTicketDataFormat(
    new DataProtectorShim(
        DataProtectionProvider.Create(dir,
            (builder) => { builder.SetApplicationName("SharedCookieApp"); })
        .CreateProtector(
            "Microsoft.AspNetCore.Authentication.Cookies." +
                "CookieAuthenticationMiddleware",
            "Identity.Application",
            "v2")))
        });

        System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
    }
}

这是 ASP.NET Core 3.1 应用程序的启动

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\test\core");

        services.AddDataProtection()
.PersistKeysToFileSystem(dir)
.SetApplicationName("SharedCookieApp");

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                // Change the options as needed
                options.Cookie.Name = ".AspNet.SharedCookie";
                options.Cookie.Path = "/";
            });
    }

标签: asp.netasp.net-coreasp.net-core-3.1cookie-authentication.net-4.8

解决方案


推荐阅读