小编KDT*_*KDT的帖子

尝试UserPrincipal.FindByIdentity时登录失败错误

我一直在寻找解决方案一段时间,但不幸的是每个线程都是死路一条.

我正在开发一个只在我们公司内部使用的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中是否完全禁用匿名访问?

c# asp.net

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net ×1

c# ×1