我的目录日很糟糕。 :)
有人能告诉我这有什么问题吗?
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方法创建用户,因此它不是权限问题。
答案 0 :(得分:3)
尝试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();
}
}
}