0

我有一个运行 ASP.NET MVC 的小型 Web 服务器。服务器与用户“abc”一起运行,但用户“abc”没有 ActiveDirectory 中的“更改”权限。

所以我必须通过 PrincipalContext 中的用户登录。

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
{

    GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

    theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

    theGroup.Save();

}

该准则确实有效。但是我不喜欢将密码从 Methode 转移到 Methode... => 在 MVC 上我有一个 SSO,服务器知道我

System.Web.HttpContext.Current.User.Identity

可以使用此信息吗?

new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???

或者我必须提供密码。以及如何最好地从视图传递到此方法。

谢谢

4

1 回答 1

1

这称为“模仿”。只要你使用的是 Windows 身份验证,你就可以使用以下WindowsIdentity.Impersonate()方法:

using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
    // Anything done here will use the user's credentials
    using (var context = new PrincipalContext(ContextType.Domain)) {
        ...
    }
}
于 2018-11-22T14:51:51.883 回答