如何在同一网络上的另一台服务器上验证我的asp.net中的活动目录用户 [英] how to authenticate active directory users in my asp.net on another server on the same network

查看:109
本文介绍了如何在同一网络上的另一台服务器上验证我的asp.net中的活动目录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC4中实现Windows身份验证

如何在同一网络上的另一台服务器上验证我的asp.net中的活动目录用户?

I am implementing windows authentication in MVC4
how to authenticate active directory users in my asp.net on another server on same network?

推荐答案

以下是LDAP身份验证的示例(您可以从 http://support.microsoft.com/获取更多详细信息kb / 316748 [ ^ ] ) :



只需传递有效的域名(正如您所说,它应该可以访问),您无需担心您的应用程序是否位于哪个域中。





public bool IsAuthenticated(String domain,String username,String pwd)

{

String domainAndUsername = domain + @\+ username;

DirectoryEntry entry = new DirectoryEntry(_path,domainAndUsername,pwd);





尝试

{//绑定o强制身份验证的原生AdsObject。

对象obj = entry.NativeObject;





DirectorySearcher search =新的DirectorySearcher(条目);





search.Filter =(SAMAccountName =+ username +);

search.PropertiesToLoad.Add(cn);

SearchResult result = search.FindOne();





if(null == result)

{

返回false;

}





//更新目录中用户的新路径。

_path = result.Path;

_filterAttribute =(String)result.Properties [cn] [0];

}

catch(exception ex)

{

抛出新的异常(错误验证用户。 + ex.Message);

}





返回true;

}
Below is the sample for LDAP authentication (you can take more details from http://support.microsoft.com/kb/316748[^]):

Just pass valid Domain name (as you said it should be reachable), you need not to worry whether you application is lying in which domain.


public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);


try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;


DirectorySearcher search = new DirectorySearcher(entry);


search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();


if(null == result)
{
return false;
}


//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}


return true;
}


这篇关于如何在同一网络上的另一台服务器上验证我的asp.net中的活动目录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆