创建本地用户

发布于 2024-08-04 08:52:30 字数 1403 浏览 4 评论 0原文

我的目录日很糟糕。 :)

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

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,但我正在尝试创建本地组,而不是 Active Directory 组。

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

I am having a bad directory day. :)

Could someone tell me what is wrong with this?

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

What i am trying to do is create a local account. When I try this code as is, it crashes when I try to set the samaccountname property:

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:

If I comment out that line, it crashes on commit with the following:

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: 

I am not sure what to think about the Source. I am on a Vista in a W2003 domain, but I'm trying to create a local group, not an active directory group.

Any ideas? I probably missed something obvious. I can create users using the GroupPricipal.Save method, so it is not a permissions issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-08-11 08:52:30

尝试此代码,我很确定它会成功;)

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

    }
    }
}

Try this code, I'm pretty sure it will do the trick ;)

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

    }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文