有没有将 ASP.NET Windows 身份验证与自定义 IPrincipal/IIdentity 对象相结合的好方法?我需要存储用户的电子邮件地址,并使用我在 AuthenticateRequest 事件期间添加到 Context.CurrentUser 的自定义 IIdentity/IPrincipal 对来进行表单身份验证。
我最好如何使用 WindowsAuthentication 来完成此任务?
有没有将 ASP.NET Windows 身份验证与自定义 IPrincipal/IIdentity 对象相结合的好方法?我需要存储用户的电子邮件地址,并使用我在 AuthenticateRequest 事件期间添加到 Context.CurrentUser 的自定义 IIdentity/IPrincipal 对来进行表单身份验证。
我最好如何使用 WindowsAuthentication 来完成此任务?
也许您可以将“ExtendedWindowsPrincipal”创建为基于 WindowsPrincipal 的派生类,然后将额外数据添加到派生类中?
这样,您的 ExtendedWindowsPrincipal 仍然会在需要 WindowsPricinpal 的任何地方被识别。
或者:由于您正在谈论使用 Windows 身份验证,因此您可能在 Windows 网络中 - 是否有某个 Active Directory 或用户数据库,您可以在其中查找您感兴趣的电子邮件地址,而不是将其存储在主体中?
马克
我最终将我最初的解决方案重构为替换 Principal 而不是我最初认为的 Identity。替换身份证明很麻烦,因为我在创建新的扩展 WindowsPrincipal 的实例时遇到了安全问题。
public class ExtendedWindowsPrincipal : WindowsPrincipal
{
private readonly string _email;
public ExtendedWindowsPrincipal(WindowsIdentity ntIdentity,
string email) : base(ntIdentity)
{
_email = email;
}
public string Email
{
get { return _email; }
}
}
在我的身份验证模块中,我替换了 HttpContext 上的主体,如下所示:
var currentUser = (WindowsIdentity)HttpContext.Current.User.Identity;
HttpContext.Current.User =
new ExtendedWindowsPrincipal(currentUser, userEmail);