我一直在寻找解决方案一段时间,但不幸的是每个线程都是死路一条.
我正在开发一个只在我们公司内部使用的C#/ ASP.net网络应用程序.IIS和我的web.config文件中的匿名访问都已关闭,强制IIS使用Windows身份验证用户(Active Dir用户).
我的问题是,以下代码完美地用于获取所需(或任何)AD用户:
using System.DirectoryServices.AccountManagement;
... other code ...
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain", "someADuser", "someADuserpassword");
UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "samaccountname");
Run Code Online (Sandbox Code Playgroud)
上面的PrincipalContext中使用的"someADuser"是当前通过窗口登录的用户,因此经过身份验证并且是有效的AD用户.使用以下代码(完全相同的用户仍然登录)给我一个"登录失败:未知的用户名或密码错误"错误:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain");
UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "samaccountname");
Run Code Online (Sandbox Code Playgroud)
似乎UserPrincipal.FindByIdentity由于某种原因没有使用登录用户的验证凭据,如果它没有在PrincipalContext对象中指定 - 我不想这样做.
由于某些原因,即使必要的设置(我希望)添加到web.config,ctx是否有可能因为某些原因没有获取登录的Windows用户:
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)
IIS中是否完全禁用匿名访问?
似乎UserPrincipal.FindByIdentity由于某种原因没有使用登录用户的验证凭据,如果它没有在PrincipalContext对象中指定 - 我不想这样做.
UserPrincipal.FindByIdentity根本不关心用户的凭据.您只是在查看该帐户是否存在.您收到错误的原因是因为默认用户凭据(即您的Web应用程序正在运行的身份)无法访问该目录,因此无法执行查找.当您将客户端的凭据传递给PrincipalContext时,问题就会消失,因为您的客户端具有可访问该目录的有效AD帐户.
您应该调查用于运行应用程序池的标识,并确保它可以访问该目录.
| 归档时间: |
|
| 查看次数: |
12398 次 |
| 最近记录: |