asp.net-identity - 身份用户 .net 4.6 和 .net core 2.2
问题描述
我有以下场景: .net 4.6 Web 表单应用程序在 fe 中运行,domain.com
用户使用身份 2.2 注册和登录。有一些管理员用户具有Administrator
. 还有一个子域 fe admin.domain.com
,在 .net core 2.2 中制作,现在我希望管理员用户只能访问子域。
我做了什么:
- 第一次尝试是生成一个链接,
admin.domain.com?email=<hashed email>&datetime=<hashed datetime>
并将此链接显示给域中的管理员用户,以便跳转到子域。然后在子域中,我尝试读取查询字符串并确定用户是否有权访问子域。在这种方法中,我遇到了很多问题,我认为这不是正确的解决方案。 - 我的第二种方法也是对子域使用 Identity 用户,但我意识到 2 个身份(.NET Framework 4.6 和 Core 2.2)是不同的,我没有设法让它工作,例如我需要已经域中的登录用户自动在子域中获得授权。此外,子域没有任何注册程序,仅存在于 domain.com
我想知道是否有一个强大的解决方案可以解决我的 2 个身份问题,目前我需要在 domain.com 中至少保留身份 2.2。
提前致谢!
解决方案
如果您想尝试第二种方法,请尝试使用此 github 存储库中的 asp.net 票证桥。我用它来促进在 asp.net 核心和 Web 表单身份验证之间共享单一身份 - 只需记住同步加密密钥......希望这会有所帮助!
您将需要创建自己的“ISecureDataFormat”实现:
public class OWINAuthenticationDataFormat<TData> : ISecureDataFormat<TData>
where TData : AuthenticationTicket
{
public OWINAuthenticationOptions Options { get; set; }
...
public string Protect(TData data)
{
return Protect(data, null);
}
..
public string Protect(TData data, string purpose)
{
string decryptionKey = Options.DecryptionKey;
string validation = Options.ValidationMethod;
string validationKey = Options.ValidationKey;
string decryption = Options.EncryptionMethod;
var claimsIdentity = data.Principal.Identity as ClaimsIdentity;
var authTicket = new OwinAuthenticationTicket(claimsIdentity, data.Properties);
// Encrypt the token
return MachineKeyTicketProtector.ProtectCookie(authTicket, decryptionKey, validationKey, decryption, validation);
}
...
public TData Unprotect(string protectedText)
{
return Unprotect(protectedText, null);
}
...
public TData Unprotect(string protectedText, string purpose)
{
string decryptionKey = Options.DecryptionKey;
string validation = Options.ValidationMethod;
string validationKey = Options.ValidationKey;
string decryption = Options.EncryptionMethod;
// Decrypt the token
var ticket = MachineKeyTicketUnprotector.UnprotectCookie(protectedText, decryptionKey, validationKey, decryption, validation);
return new AuthenticationTicket(new System.Security.Claims.ClaimsPrincipal(ticket.Identity), ticket.Properties, "") as TData;
}
}
之后,在添加 cookie 身份验证时使用它(仍在 asp.net 核心应用程序中):
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, opts =>
{
opts.Cookie = new CookieBuilder()
{
Domain = CookieDomain,
Name = CookieName,
Path = CookiePath,
SecurePolicy = CookieSecurePolicy.Always
};
opts.TicketDataFormat = new OWINAuthenticationDataFormat<AuthenticationTicket>()
{
Options = new OWINAuthenticationOptions()
{
DecryptionKey = DecryptionKey,
EncryptionMethod = DecryptionAlgorithm,
ValidationKey = ValidationKey,
ValidationMethod = ValidationAlgorithm
}
};
});
请记住在两个应用程序中使用相同的签名密钥和算法!
推荐阅读
- google-apps-script - 如何在更改之前打印值?(谷歌脚本)
- css - 将 javascript 中的 css 变量颜色动态添加到 Sass 函数
- google-sheets - 使用数组公式跳过空白值和公式的顺序行编号
- excel - 将 Excel 连接到 Heroku Postgres 数据库
- oracle - Oracle SQL Developer ERD 不会自动包含关系表
- css - 将鼠标悬停在图像上时应用变换
- python - 根据2个数据框关系在特定列中写入超过nan值的数据框列
- java - 使用 LibHoudini 在 x86 架构上运行本机 ARM 库
- ios - 我是否必须注册并向 Apple Developer 付款才能创建适用于 iOS 的应用程序
- python - 损失值在 LSTM 训练中变化