【问题标题】:Creating local users创建本地用户
【发布时间】:2009-09-03 23:26:59
【问题描述】:

我的目录很糟糕。 :)

谁能告诉我这是怎么回事?

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: 

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

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

【问题讨论】:

    标签: c# .net adsi


    【解决方案1】:

    试试this code,我很确定它会成功的;)

    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();
    
        }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 2013-02-16
      • 2010-09-27
      • 2010-09-27
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多