asp.net – System.DirectoryServices – 服务器不可操作
| 我收到一个网站的错误,我使用 
 Windows身份验证. 
  
  奇怪的东西: >仅当用户尚未保存到数据库(新的未知用户)时才会发生 这是我在日志邮件中得到的: 
 这是我如何实现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();
    }
}如果需要更多的代码示例,请告诉我. 解决方法
 您的问题是您正在使用“简单”域名进行绑定 – 这在LDAP中不起作用.实际上,如果你尝试绑定到LDAP:// MyDomain,你真正在做的是试图绑定到名为MyDomain的服务器. 
  
  您需要一个有效的LDAP绑定字符串,如LDAP:// dc = yourdomain,dc = local或something. 要了解您的默认LDAP绑定上下文是什么,请使用此代码段: DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
if (deRoot != null)
{
   string defaultNamingContext = deRoot.Properties["defaultNamingContext"].Value.ToString();
}一旦你有这个字符串 – 将它用作你的LDAP服务器的绑定字符串. 如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间.在这里阅读全文: > Managing Directory Security Principals in the .NET Framework 3.5 基本上,您可以定义域上下文并轻松查找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....     
}新的S.DS.AM让您很容易在广告中与用户和群体一起玩耍! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
- asp.net – IIS返回找不到.mp4文件的404
- asp.net-mvc – 当您使用带有淘汰赛的JS视图模型时,MVC有什
- asp.net – 是否可以完全在代码中配置ELMAH?
- ASP.Net C#ResolveClientUrl里面的类
- asp.net – 如何测试开发机器上的子域? abc.localhost
- asp.net – IE在IIS7中打开脚本,打开静态压缩
- asp.net-mvc-3 – MVC 3(Razor) – 使用Button事件调用Cont
- asp.net – 将数据绑定到网格视图
- asp.net – coldfusion和.net上的单点登录
- asp.net-mvc – ASP.Net T4MVC文件未自行更新
- asp.net-mvc – 注入AutoMapper实例
- 文件上传 – 在ASP.NET Core中使用Axios和Vue.JS
- asp.net – 无法识别的配置部分
- asp.net – 如何从TableAdapter中检索存储过程返
- asp.net-core – 在Asp.Net Core中注入DbContext
- IIS上的ASP.NET Core 2.0错误502.5
- ASP.NET C#Active Directory – 查看用户密码到期
- asp.net – 当用户有多个角色时,位置授权如何工作
- asp.net-mvc – Basic Umbraco 6.1.1 SurfaceCon
- .net – 编译器错误消息:编译器失败,错误代码为
