為了獲得概述,我有一個舊的 .NET Framework 4.7.2 網站,其中包含大量 .aspx 檔案,我有一個新的 .NET Core WebApp。兩者都在具有不同埠的同一域下的同一臺服務器上運行。
我現在想登錄到 .NET Core 網站并且也能夠在 .NET Framework 網站上使用身份驗證令牌。我嘗試使用 .NET Core 網站生成的 Cookie 通過 Owin 在 .NET Framework 網站上進行身份驗證,但我找不到任何方法來讓它作業。
有沒有人有關于如何實作這一點的想法?我需要進行一次身份驗證,并且能夠在兩個網站上都經過身份驗證。
uj5u.com熱心網友回復:
要在 ASP.NET 4.x 應用程式和 ASP.NET Core 應用程式之間共享身份驗證 cookie,首先,請按照以下步驟配置 ASP.NET Core 應用程式:
向您的應用程式添加身份驗證
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(); //other services... }在您的
Configure方法中,使用CookieAuthenticationOptions為 cookie 設定資料保護服務app.UseCookieAuthentication(new CookieAuthenticationOptions { DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\")) });
然后按照以下步驟配置您的 ASP.NET 4.7.2 應用程式:
將該包安裝
Microsoft.Owin.Security.Interop到您的 ASP.NET 4.7.2 應用程式中。在 中
Startup.Auth.cs,找到對 的呼叫UseCookieAuthentication,它通常如下所示:app.UseCookieAuthentication(new CookieAuthenticationOptions { // ... });將呼叫修改
UseCookieAuthentication如下,更改AuthenticationType和CookieName以匹配 ASP.NET Core cookie 身份驗證中間件的 和 ,并提供已初始化到密鑰存盤位置的 DataProtectionProvider 的實體。app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", CookieName = ".AspNetCore.Cookies", // CookiePath = "...", (if necessary) // ... TicketDataFormat = new AspNetTicketDataFormat( new DataProtectorShim( DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\")) .CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Cookies", "v2"))) });
DirectoryInfo 必須指向您將 ASP.NET Core 應用程式指向的相同存盤位置,并且應該使用相同的設定進行配置。
在 中
IdentityModels.cs,將呼叫更改ApplicationUserManager.CreateIdentity為使用與 cookie 中間件中相同的身份驗證型別。public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = manager.CreateIdentity(this, "Cookies"); // ... }
參考:
在 ASP.NET 應用程式之間共享身份驗證 cookie
在 ASP.NET 4.x 和 ASP.NET Core 應用程式之間共享身份驗證 cookie
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364111.html
