asp.net - 在 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 = "/";
});
}
解决方案
推荐阅读
- sqlite - Sqlite:查找具有共同值的记录对
- json - 如何解码嵌入的json?
- sql-server - 使用 Spark 在 SQL SERVER 数据库中使用 HIVE 中的数据更新表
- visual-studio-code - 如何在 VSCode 中为同时运行的两个不同 Python 脚本拆分终端
- json - JSON 到 powershell
- microsoft-cognitive - 由于转录参考,无法删除自定义语音模型
- javascript - 代码按我想要的方式工作,但出现“未捕获的类型错误”
- mongodb - Celery MongoClient 分叉前开启
- python - 一个 HTTP GET 请求返回 302,但位置标头与我最初传递的相同
- image - 9 Flutter 中的补丁替代方案?