2009-09-03 8 views
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メソッドを使用してユーザーを作成できるため、アクセス許可の問題ではありません。

答えて

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

    } 
    } 
} 
関連する問題