标签: directoryservices

如何获取来宾/管理员的本地组名?

题:

我使用http://support.microsoft.com/kb/306273上的代码

添加Windows用户.问题是我需要将用户添加到组,但组名已本地化.

例如,MS-example使用英文计算机,这意味着您可以像这样获取访客组:grp = AD.Children.Find("Guests","group")

但是在非英语计算机上,"访客"组名是本地化的,例如在我的德语操作系统上,来宾的组名是"Gäste".

这意味着支持示例在我的计算机上运行我需要将该行更改为grp = AD.Children.Find("Gäste","group")

然后它工作.

现在,如果操作系统是任何其他语言,我如何找到访客用户的名称?或者如何从sid获取来宾用户名?

注意:.NET 2.0,而不是3.0或3.5

.net c# vb.net directoryservices active-directory

15
推荐指数
2
解决办法
9390
查看次数

System.DirectoryServices - 服务器无法运行

我在网站上收到错误,我使用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 …
Run Code Online (Sandbox Code Playgroud)

asp.net directoryservices active-directory windows-authentication

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

确定用户是否在.NET 4.0应用程序的AD组中

我试图确定用户是否是内部ASP.NET 4.0应用程序的Active Directory(AD)组的成员.在用户不是AD组成员的情况下,下面的代码在最后一行(return语句)上抛出"尝试访问已卸载的appdomain"异常错误.

public static bool IsInADGroup(string userName, string groupName)
{
    var principalContext = new PrincipalContext(ContextType.Domain);
    UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
    if (userPrincipal == null)
        return false;

    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
    if (groupPrincipal == null)
        return false;

      return userPrincipal.IsMemberOf(groupPrincipal);
}
Run Code Online (Sandbox Code Playgroud)

有关如何修复或其他解决方法的任何想法?

.net c# directoryservices userprincipal

14
推荐指数
2
解决办法
8168
查看次数

如何在复杂环境中使用FQDN获取NETBIOS域名

从完全限定的Active Directory域名获取NETBIOS域名有时是一项繁琐的工作.我在这里找到了一个好的答案.

在具有多个林的环境中,如果PC不在您正在查询的林中,则此方法将不起作用.这是因为LDAP://RootDSE将查询计算机域的信息.

有人可能会问:为什么这么复杂?只需在检索到的第一个点之前使用该名称:

ActiveDirectory.Domain.GetComputerDomain().Name;
Run Code Online (Sandbox Code Playgroud)

或者只是获取用户的域名:

Environment.GetEnvironmentVariable("USERDOMAIN");
Run Code Online (Sandbox Code Playgroud)

要么

Environment.UserDomainName;
Run Code Online (Sandbox Code Playgroud)

但是NETBIOS域名可能完全不同,您或您的计算机可能位于不同的域或林中!所以这种方法只能在简单的环境中使用.

DJ KRAZE的解决方案只需要一个小的修改就可以进行跨域查询.这假设了一种信任关系!

private string GetNetbiosDomainName(string dnsDomainName)
{
      string netbiosDomainName = string.Empty;

      DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE",dnsDomainName));

      string configurationNamingContext = rootDSE.Properties["configurationNamingContext"][0].ToString();

      DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Partitions," + configurationNamingContext);

      DirectorySearcher searcher = new DirectorySearcher(searchRoot);
      searcher.SearchScope = SearchScope.OneLevel;
      searcher.PropertiesToLoad.Add("netbiosname");
      searcher.Filter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", dnsDomainName);

      SearchResult result = searcher.FindOne();

      if (result != null)
      {
        netbiosDomainName = result.Properties["netbiosname"][0].ToString();
      }

      return netbiosDomainName;
    }
Run Code Online (Sandbox Code Playgroud)

c# directoryservices active-directory

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

使用DirectoryServices从C#连接到LDAP

我正在尝试连接到运行LDAP的edirectory 8.8服务器.我将如何在.Net中这样做呢?我是否仍然可以使用System.DirectoryService中的类,例如DirectoryEntry和DirectorySearcher,或者它们是AD特定的吗?我是否需要以不同方式指定"连接字符串"?

我正在尝试类似下面的代码,但它似乎不起作用......

DirectoryEntry de = new DirectoryEntry ("LDAP://novellBox.sample.com","admin","password",AuthenticationTypes.None);
DirectorySearcher ds = new DirectorySearcher(de);
var test = ds.FindAll();
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c# directoryservices novell ldap edirectory

13
推荐指数
2
解决办法
9万
查看次数

System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity的奇怪问题

我们正在编写一个允许用户通过Intranet上的Web应用程序更改其帐户密码的系统.

起初,一切似乎都在顺利进行.在开发期间,我们的测试帐户的密码可以毫无问题地更改.

然而,当我们制作系统时,我们开始遇到问题.以下是症状:

  1. 一开始,一切都很好.用户可以更改其密码.
  2. 在某些时候,UserPrincipal.FindByIdentity中出现以下错误:"System.Runtime.InteropServices.COMException:身份验证机制未知."
  3. 从那时起,尝试通过Web应用程序更改密码会导致错误:"System.Runtime.InteropServices.COMException:服务器无法运行."
  4. 如果我手动回收应用程序池,一切似乎都会自行解决,直到更多错误开始发生...即,该过程在第1阶段重新开始.

这是相关的代码片段:


    private static PrincipalContext CreateManagementContext() {
        return new PrincipalContext(
            ContextType.Domain, 
            ActiveDirectoryDomain, 
            ActiveDirectoryManagementAccountName,
            ActiveDirectoryManagementAccountPassword);
    }


    private static void ChangeActiveDirectoryPasword(string username, string password) {
        if (username == null) throw new ArgumentNullException("username");
        if (password == null) throw new ArgumentNullException("password");

        using (var context = CreateManagementContext())
        using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)) {
            user.SetPassword(password);
        }
    }
Run Code Online (Sandbox Code Playgroud)

关于为什么会发生这种情况的任何线索?谷歌搜索没有提供任何真正有用的信息,MSDN上的文档也没有.

directoryservices memory-leaks ldap active-directory account-management

13
推荐指数
1
解决办法
5581
查看次数

如何注册System.DirectoryServices以用于SQL CLR用户功能?

我正在移植一个旧的3 2-bit COM组件,它是VB6为了读写Active Directory服务器而编写的.新解决方案将在C#并将使用SQL CLR用户功能.

我尝试部署的程序集SQL Server包含对它的引用System.DirectoryServices.该项目确实编译没有任何错误,但SQL Server由于以下错误,我无法将程序集部署到该程序集:

Error: Assembly 'system.directoryservices, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.' was not found in the SQL catalog.

System.DirectoryServices在SQL Server上注册的正确步骤是什么?

.net directoryservices sqlclr active-directory sql-server-2008

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

PrincipalSearchResult <T>是否自动处理其集合中的所有元素?

在MSDN文档中找不到任何关于此的内容.

也就是这样做,说:

using(PrincipalSearcher searcher = ...)
{
    foreach (var principal in searcher.FindAll())
    {
        ... do something ...
    } // The PrincipalSearchResult<T> returned by searcher.FindAll is disposed here
}
Run Code Online (Sandbox Code Playgroud)

这是我见过的大多数例子,或者我应该这样做:

using(PrincipalSearcher searcher = ...)
{
    foreach(var principal in searcher.FindAll())
    {
        using (principal)
        {
            // ... do something ...
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

后者(在迭代期间明确地处理每个项目)看起来"更安全" - 即符合指南以明确处理所有IDisposable对象 - 但它有点凌乱; 例如,它排除了使用LINQ迭代搜索结果.

回应@Rup的评论:

你可以编写一个从父迭代器返回一个结果的yield迭代器

是的,我认为这样可以启用LINQ.像下面的扩展方法:

public static IEnumerable<T> EnumerateAndDispose<T>(this IEnumerable<T> collection) where T : IDisposable
{
    foreach (T item in collection)
    {
        using (item)
        {
            yield …
Run Code Online (Sandbox Code Playgroud)

.net directoryservices principalsearcher

13
推荐指数
2
解决办法
1987
查看次数

如何从Active Directory获取组织单位列表?

我查看了DirectoryServices类,它似乎是我需要的,但我似乎无法找到获取组织单位集合所需的类/方法.

你们能提出一些建议吗?

c# directoryservices

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

GroupPrincipal.GetMembers在组(或子组,如果递归)包含ForeignSecurityPrincipal时失败

对于遇到同样问题的人来说,这不是一个问题.

发生以下错误:

System.DirectoryServices.AccountManagement.PrincipalOperationException: An error (87) occurred while enumerating the groups. The group's SID could not be resolved. 
at System.DirectoryServices.AccountManagement.SidList.TranslateSids(String target, IntPtr&#91;&#93; pSids) 
at System.DirectoryServices.AccountManagement.SidList.ctor(List`1 sidListByteFormat, String target, NetCred credentials) 
at System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.TranslateForeignMembers()
Run Code Online (Sandbox Code Playgroud)

运行以下代码并且组或子组包含ForeignSecurityPrincipal时:

private static void GetUsersFromGroup()
{
    var groupDistinguishedName = "CN=IIS_IUSRS,CN=Builtin,DC=Domain,DC=com";
    //NB: Exception thrown during iteration of members rather than call to GetMembers.    
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domain", "Username", "Password"))
    {
        using (GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, groupDistinguishedName))
        {                    
            using (var searchResults = groupPrincipal.GetMembers(true))//Occurs when false also. …
Run Code Online (Sandbox Code Playgroud)

c# directoryservices active-directory

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