1

我有一个糟糕的目录日。:)

有人能告诉我这有什么问题吗?

groupName = "Monkey";
...
using (DirectoryEntry directoryEntryObject = new DirectoryEntry("WinNT://" + Environment.MachineName, "", "", AuthenticationTypes.Secure))
{
     using (DirectoryEntry group = directoryEntryObject.Children.Add("CN=" + groupName.Trim(), "group"))
      {
            group.Properties["sAMAccountName"].Value = groupName;
            group.CommitChanges();
       }
}

我想做的是创建一个本地帐户。当我按原样尝试此代码时,当我尝试设置 samaccountname 属性时它会崩溃:

System.Runtime.InteropServices.COMException occurred
  Message="The directory property cannot be found in the cache.\r\n"
  Source="Active Directory"
  ErrorCode=-2147463153
  StackTrace:
       at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.PutEx(Int32 lnControlCode, String bstrName, Object vProp)
  InnerException:

如果我注释掉该行,它会在提交时崩溃,并显示以下内容:

System.Runtime.InteropServices.COMException occurred
  Message="The specified username is invalid. (Exception from HRESULT: 0x8007089A)"
  Source="System.DirectoryServices"
  ErrorCode=-2147022694
  StackTrace:
       at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo()
  InnerException: 

我不确定如何看待源。我在 W2003 域中的 Vista 上,但我正在尝试创建一个本地组,而不是一个活动目录组。

有任何想法吗?我可能错过了一些明显的东西。我可以使用 GroupPricipal.Save 方法创建用户,所以这不是权限问题。

4

1 回答 1

3

试试这个代码,我很确定它会成功;)

using System;
using System.DirectoryServices;

class Class1
{
    static void Main(string[] args)
    {
    try
        {
     DirectoryEntry AD = new DirectoryEntry("WinNT://" + 
                         Environment.MachineName + ",computer");
     DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
     NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
     NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
     NewUser.CommitChanges();
     DirectoryEntry grp;

     grp = AD.Children.Find("Guests", "group");
     if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}
     Console.WriteLine("Account Created Successfully");
     Console.ReadLine();
    }
    catch (Exception ex)
    {
     Console.WriteLine(ex.Message);
     Console.ReadLine();

    }
    }
}
于 2009-09-04T02:14:04.573 回答