使用Windows身份验证时,IIS如何获取WindowsPrincipal上的数据?

dwbf0jvd  于 2023-06-06  发布在  Windows
关注(0)|答案(1)|浏览(465)

对于使用Windows身份验证的内部应用程序,我可以使用以下代码在C#中获取WindowsPrincipal对象:
var winPrincipal = (WindowsPrincipal)HttpContext.Current.User;
此信息(包括Sam名称和用户所在的域组,这些信息包含在此对象中)是否直接从用户的计算机传送到IIS,或者IIS是否必须查询LDAP?
我发现从应用程序代码中查询LDAP不是一个自由的操作,所以我想知道如果IIS正在查询LDAP来构造WindowsPrincipal对象,这是否会给请求增加一些开销,如果它不查询LDAP,那么如果我只信任IIS的WindowsPrincipal对象而不查询LDAP(即,如果IIS不查询LDAP,则会引入任何安全问题)。攻击者是否可能在请求时将他们实际上不属于的组添加到他们的票证中)?

6ju8rftf

6ju8rftf1#

Windows身份验证使用用户计算机上的access token,但服务器会验证它是否有效。
身份验证组存储在令牌中,因此不会发出LDAP请求。
如果您需要用户帐户中未存储在令牌中的任何信息,则需要自己发出LDAP请求。最有效的方法是使用SID直接绑定到用户的对象,您已经拥有了SID:

var identity = (WindowsIdentity) HttpContext.Current.User.Identity;

var user = new DirectoryEntry($"LDAP://<SID={identity.User.Value}>");

//Ask for only the attributes you want to read.
//If you omit this, it will end up getting every attribute with a value,
//which is unnecessary.
user.RefreshCache(new [] { "givenName", "sn" });

var firstName = user.Properties["givenName"].Value;
var lastName = user.Properties["sn"].Value;

如果您担心发出LDAP请求时的效率问题,我在不久前写了一篇文章讨论了这个问题:Active Directory: Better performance

相关问题