我有以下情况:.net 4.6 Web窗体应用程序正在运行中。 domain.com
,用户使用身份2.2注册和登录。有些管理员用户具有Administrator
的角色。还有一个子域f.e. admin.domain.com
,在.net core 2.2中制作,现在我希望Administrator用户只能在子域中访问。
我做了什么:
admin.domain.com?email=<hashed email>&datetime=<hashed datetime>
之类的链接,并向域中的Admin用户显示此链接,以跳转到子域。然后,在子域中,我尝试读取查询字符串并确定用户是否有权访问子域。通过这种方法,我遇到了很多问题,我认为这不是正确的解决方案。我想知道我的2个身份是否有一个可靠的解决方案,目前我至少需要在domain.com中保留身份2.2。
提前感谢!
如果您想尝试第二种方法,请尝试使用this github repository中的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
}
};
});
请记住在两个应用程序中使用相同的签名密钥和算法!