Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试UserPrincipal.FindByIdentity时出现登录失败错误_C#_Asp.net - Fatal编程技术网

C# 尝试UserPrincipal.FindByIdentity时出现登录失败错误

C# 尝试UserPrincipal.FindByIdentity时出现登录失败错误,c#,asp.net,C#,Asp.net,我已经为此寻找了一段时间的解决方案,但不幸的是,每一个线程都有一个死胡同 我正在开发一个C#/ASP.net web应用程序,它只供我们公司内部使用。IIS和my web.config文件中的匿名访问都已关闭,强制IIS使用windows身份验证用户(Active Dir用户) 我的问题是,以下代码可以完美地获得所需(或任何)广告用户: using System.DirectoryServices.AccountManagement; ... other code ... Principal

我已经为此寻找了一段时间的解决方案,但不幸的是,每一个线程都有一个死胡同

我正在开发一个C#/ASP.net web应用程序,它只供我们公司内部使用。IIS和my web.config文件中的匿名访问都已关闭,强制IIS使用windows身份验证用户(Active Dir用户)

我的问题是,以下代码可以完美地获得所需(或任何)广告用户:

using System.DirectoryServices.AccountManagement;

... other code ...

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain", "someADuser", "someADuserpassword");
UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "samaccountname");
上面PrincipalContext中使用的“someADuser”是通过windows登录的当前用户,因此经过身份验证,是有效的AD用户。使用以下代码(完全相同的用户仍在登录)会出现“登录失败:未知用户名或错误密码”错误:

如果PrincipalContext对象中未指定UserPrincipal.FindByIdentity,则出于某种原因,UserPrincipal.FindByIdentity似乎不会使用登录用户的已验证凭据—我不想这样做

ctx是否可能出于某种原因没有接收登录的Windows用户,即使在web.config中添加了必要的设置(我希望是这样):

<authentication mode="Windows"/>
<authorization>
  <deny users="?"/>
</authorization>

在IIS中完全禁用匿名访问

如果PrincipalContext对象中未指定UserPrincipal.FindByIdentity,则出于某种原因,UserPrincipal.FindByIdentity似乎不会使用登录用户的已验证凭据—我不想这样做

UserPrincipal.FindByIdentity根本不关心用户的凭据。您只是在执行查找,以查看该帐户是否存在。出现错误的原因是默认用户凭据(即web应用程序运行时的身份)无权访问目录,因此无法执行查找。当您将客户机的凭据传递给PrincipalContext时,问题就消失了,因为您的客户机具有访问该目录的有效AD帐户


您应该调查运行应用程序池所使用的身份,并确保它可以访问该目录。

相当烦人,因为如果关闭匿名访问,当前主体将默认为登录到windows的用户。事实证明,这并不像@RogerN所说的那样

使用@TheKingDave提到的以下语句,它基本上模拟了登录到windows的用户,并使当前线程在其主体上运行,而不是在“ASP”(在我的例子中)帐户上运行

因为我们域中的所有用户都具有对Active Directory的查询/读取访问权限,所以获取关于他们的更多详细信息应该不是问题,这正是我首先想要的

结束代码(正在测试):


希望这对将来有帮助!thx!;-)

我做了大量的搜索/研究来解决这个问题,但没有任何效果,最后,我所做的只是将
@
添加到服务器名、容器、用户名和密码中,如下所示:

app.CreatePerOwinContext(() => new PrincipalContext(ContextType.Domain, @"abc.net", @"OU=Customers,DC=abc,DC=net", ContextOptions.SimpleBind, @"abcnet\authuser", @"!$%MyPassword"));

它成功了。啊

所以我找到了如何传递模拟身份。我曾经遇到过这样的情况,作为一名管理员,我想自动解锁一个用户帐户。我必须将用户名转换成变量,然后像这样将它们传递给函数:

string accountname = @"domain\username";
string admin = "adminusername";
string domain = Environment.UserDomainName;
string password = "password";
string dc = "WIN2K8DC1";  // example host name of domain controller, could use IP

// This determines the domain automatically, no need to specify
// Use the constructor that takes the domain controller name or IP, 
// admin user name, and password
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc, admin, password); 

UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, accountname)

if (winuser != null)
{
    WindowsImpersonationContext wic = Impersonation.doImpersonation(admin, domain, password); //class/function that does the logon of the user and returns the WIC
    if (wic != null)
    {
        winuser.UnlockAccount();
    }
} 
可以在MSDN上找到可用于返回WIC的模拟类功能:

我不知道您为什么要以编程方式执行此操作,为什么不使用模拟?您可以设置为特定的广告用户吗?这就是你想要做的吗?@TheKingDave,我需要从广告中获得更多关于登录用户的信息(帐户锁定/启用,guid等),因为这个应用程序将托管在服务器上,被许多域内的人访问。希望我能理解你的问题并回答清楚?Thx用于响应…在创建PrincipalContext对象后,
ctx.UserName
是什么?好问题@jadarnel27,它是空的或“”。我的印象是,如果在任何地方都禁用匿名访问,上下文对象将默认为登录的windows用户,而事实似乎并非如此。甚至添加ContextOptions。与PrincipalContext进行协商,这正是我之前的评论所建议的,不起作用。我听到你在说@RogerN,也许我完全不知道IIS是如何工作的(为我辩护,我第一次不得不处理广告和类似的问题),但我认为在IIS中关闭匿名访问并配置web应用的web.config文件以拒绝匿名访问,自动落在从aspx页面发起“线程”调用的用户标识上?哦,IIS中的“集成Windows身份验证”也从MS站点()打开:“如果用户名和密码参数均为null,则使用当前主体的默认凭据”。所以理论上我的代码应该可以工作,但事实并非如此。对于web应用程序,应用程序的身份和用户的身份是有区别的。检查User.Identity.Name和System.Security.Principal.WindowsIdentity.GetCurrent().Name。我想你会发现它们是不同的。他们相信,当您创建PrincipalContext而不指定凭据时,将使用后者。您可能是对的。。。current principal或UserPrincipal.current是ASP.net web应用程序的内置ASP用户,这意味着如果未指定任何用户,则默认为该用户。甚至可以将其默认设置为windows身份验证登录用户吗?这对我不起作用。是否要共享有关应用程序池设置的详细信息?不确定是否正确@codebased,但我的应用程序池是asp.net 4,具有在网络服务标识下运行的经典托管管道。在我的例子中,只有windows auth作为应用程序在默认网站下运行。希望有帮助?什么类型的变量是
app
?它在MVC
Startup
configuration类中的
public void ConfigureAuth(IAppBuilder app)
附加值来自
Properties.Settings.Default.DomainNameapp.CreatePerOwinContext(() => new PrincipalContext(ContextType.Domain, @"abc.net", @"OU=Customers,DC=abc,DC=net", ContextOptions.SimpleBind, @"abcnet\authuser", @"!$%MyPassword"));
string accountname = @"domain\username";
string admin = "adminusername";
string domain = Environment.UserDomainName;
string password = "password";
string dc = "WIN2K8DC1";  // example host name of domain controller, could use IP

// This determines the domain automatically, no need to specify
// Use the constructor that takes the domain controller name or IP, 
// admin user name, and password
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc, admin, password); 

UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, accountname)

if (winuser != null)
{
    WindowsImpersonationContext wic = Impersonation.doImpersonation(admin, domain, password); //class/function that does the logon of the user and returns the WIC
    if (wic != null)
    {
        winuser.UnlockAccount();
    }
}