C# 在.net 4x和.net 5应用程序之间共享身份验证Cookie-访问用户权利主张
我遵循了本文中建议的设置 我通过登录.net 4x应用程序设置身份验证cookie,然后尝试访问由.net 5核心应用程序上的C# 在.net 4x和.net 5应用程序之间共享身份验证Cookie-访问用户权利主张,c#,.net,asp.net-core,authentication,cookies,C#,.net,Asp.net Core,Authentication,Cookies,我遵循了本文中建议的设置 我通过登录.net 4x应用程序设置身份验证cookie,然后尝试访问由.net 5核心应用程序上的[Authorize]attibute保护的页面类。此操作失败,我被引导回登录页面。如果我删除[Authorize]属性并访问和解密共享身份验证cookie,我可以在AuthenticationTicket中看到由.net 4x应用程序创建的用户和声明(请参见下面的代码)-但在尝试访问页面中的ClaimsPrincipal用户时。此用户没有cookie中的任何详细信息。
[Authorize]
attibute保护的页面类。此操作失败,我被引导回登录页面。如果我删除[Authorize]
属性并访问和解密共享身份验证cookie,我可以在AuthenticationTicket中看到由.net 4x应用程序创建的用户和声明(请参见下面的代码)-但在尝试访问页面中的ClaimsPrincipal用户时。此用户没有cookie中的任何详细信息。var user=\u userManager.GetUserAsync(user).Result代码>始终为空
var dataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\temp\keyring"));
var cookieManager = new ChunkingCookieManager();
var cookie = cookieManager.GetRequestCookie(HttpContext, ".AspNetCore.SharedCookie");
var dataProtector = dataProtectionProvider.CreateProtector
("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Identity.Application", "v2");
//Get teh decrypted cookies as a Authentication Ticket
TicketDataFormat ticketDataFormat = new TicketDataFormat(dataProtector);
AuthenticationTicket ticket = ticketDataFormat.Unprotect(cookie);
var user = _userManager.GetUserAsync(User).Result;
下面是.net 5应用程序中startup.cs文件中的configureservices代码
....
services.AddDbContext<RosterDBContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("databaseconnection")
, sqlServerOptions => sqlServerOptions.CommandTimeout(120)
);
options.EnableSensitiveDataLogging();
});
services.AddIdentity<RosterUser, RosterRole>()
.AddEntityFrameworkStores<RosterDBContext>();
services.Configure<SecurityStampValidatorOptions>
(
options => {
options.ValidationInterval = TimeSpan.FromSeconds(10);
}
);
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp\keyring"));
services.AddAuthentication("Identity.Application")
.AddCookie(config =>
{
config.Cookie.Name = ".AspNetCore.SharedCookie";
config.Cookie.Path = "/";
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Identity/Account/Login/";
});
services.AddHttpContextAccessor();
services.AddRazorPages();
。。。。
services.AddDbContext(选项=>
{
options.UseSqlServer(Configuration.GetConnectionString(“数据库连接”)
,sqlServerOptions=>sqlServerOptions.CommandTimeout(120)
);
options.EnableSensitiveDataLogging();
});
服务.额外性()
.AddEntityFrameworkStores();
服务。配置
(
选项=>{
options.ValidationInterval=TimeSpan.FromSeconds(10);
}
);
services.AddDataProtection()
.persistenkeystem(新目录信息(@“c:\temp\keyring”);
services.AddAuthentication(“Identity.Application”)
.AddCookie(配置=>
{
config.Cookie.Name=“.AspNetCore.SharedCookie”;
config.Cookie.Path=“/”;
});
services.configureApplicationOK(选项=>
{
options.LoginPath=“/Identity/Account/Login/”;
});
AddHttpContextAccessor();
services.AddRazorPages();
对于任何看到这一点的人,我都能够解决这个问题。我删除了这些行并替换了services.AddIdentity().AddEntityFrameworkStores()代码>
以下是:
services.AddIdentityCore().AddRoles().AddSignInManager().AddEntityFrameworkStores()代码>
我删除了我自己的身份用户,以消除因该用户而产生的任何问题,并使用了AddIdentityCore方法,而不是AddIdentity。这停止了我不断收到的错误,说明“方案已经存在:Identity.Application”因为这一行.AddCookie(“Identity.Application”
)
我还将应用程序名称添加到两个应用程序中:
.net 4x,(builder)=>{builder.SetApplicationName(“cms应用程序”);}
和.net核心应用程序中的.PersistKeySystem(新目录信息(@“c:\temp\keyring”).SetApplicationName(“cms应用程序”)代码>对于任何看到这一点的人,我都能够解决这个问题。我删除了这些行并替换了services.AddIdentity().AddEntityFrameworkStores()代码>
以下是:
services.AddIdentityCore().AddRoles().AddSignInManager().AddEntityFrameworkStores()代码>
我删除了我自己的身份用户,以消除因该用户而产生的任何问题,并使用了AddIdentityCore方法,而不是AddIdentity。这停止了我不断收到的错误,说明“方案已经存在:Identity.Application”因为这一行.AddCookie(“Identity.Application”
)
我还将应用程序名称添加到两个应用程序中:
.net 4x,(builder)=>{builder.SetApplicationName(“cms应用程序”);}
和.net核心应用程序中的.PersistKeySystem(新目录信息(@“c:\temp\keyring”).SetApplicationName(“cms应用程序”)代码>