Tai*_*har 15 asp.net directoryservices active-directory windows-authentication
我在网站上收到错误,我使用Windows身份验证.
奇怪的事情:
这是我在日志邮件中得到的:
来源:System.DirectoryServices
消息:服务器无法运行.
跟踪:
在System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)
在System.DirectoryServices.DirectoryEntry.Bind()
在System.DirectoryServices.DirectoryEntry.get_AdsObject()
在System.DirectoryServices.DirectorySearcher.FindAll(布尔findMoreThanOne)
在的System.DirectoryServices
.DirectorySearcher.FindOne ()在Smarthouse.Labs.DataAccess.UserListManager.SaveUser(String windowsUserName)
这是我实现DirectorySearch的方式:
private void SaveUser(string windowsUserName)
{
string[] domainAndUser = windowsUserName.Split('\\');
string domain = domainAndUser[0];
string username = domainAndUser[1];
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);
DirectorySearcher search = new DirectorySearcher(entry);
try
{
// Bind to the native AdsObject to force authentication.
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
if (result == null)
{
throw new Exception("No results found in Windows authentication.");
}
User userToSave = new User();
userToSave.FirstName = (String) result.Properties["givenName"][0];
userToSave.LastName = (String) result.Properties["sn"][0];
userToSave.Email = (String) result.Properties["mail"][0];
userToSave.Username = windowsUserName;
userToSave.Guid = Guid.NewGuid();
SaveUser(userToSave);
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message, ex);
}
finally
{
//Dispose service and search to prevent leek in memory
entry.Dispose();
search.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要更多代码示例,请告诉我.
mar*_*c_s 20
您的问题是您使用"普通"域名进行绑定 - 这在LDAP中不起作用.实际上,如果你试图绑定LDAP://MyDomain
,你真正在做的是尝试绑定到被调用的服务器MyDomain
.
您需要一个有效的LDAP绑定字符串 - 类似的LDAP://dc=yourdomain,dc=local
东西.
要找出您的默认LDAP绑定上下文是什么,请使用以下代码段:
DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
if (deRoot != null)
{
string defaultNamingContext = deRoot.Properties["defaultNamingContext"].Value.ToString();
}
Run Code Online (Sandbox Code Playgroud)
获得该字符串后 - 将其用作LDAP服务器的绑定字符串.
如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间.在这里阅读所有相关内容:
基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:
// set up domain context -- no domain name needed, uses default domain
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if(user != null)
{
// do something here....
}
Run Code Online (Sandbox Code Playgroud)
新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!
归档时间: |
|
查看次数: |
34674 次 |
最近记录: |