2010-09-16 229 views
9

我有這樣的代碼來創建一個本地Windows用戶創建本地用戶帳戶

public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires) 
    { 

     try 
     { 
      PrincipalContext context = new PrincipalContext(ContextType.Machine); 
      UserPrincipal user = new UserPrincipal(context); 
      user.SetPassword(password); 
      user.DisplayName = displayName; 
      user.Name = username; 
      user.Description = description; 
      user.UserCannotChangePassword = canChangePwd; 
      user.PasswordNeverExpires = pwdExpires; 
      user.Save(); 


      //now add user to "Users" group so it displays in Control Panel 
      GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users"); 
      group.Members.Add(user); 
      group.Save(); 

      return true; 
     } 
     catch (Exception ex) 
     { 
      LogMessageToFile("error msg" + ex.Message); 
      return false; 
     } 

    } 

我想這在我的機器上正常工作。但後來我把它放在Windows服務器上。並試圖在那裏創建一個用戶。

首先,我得到了錯誤「常規訪問被拒絕錯誤」 ,所以我取得了用戶的管理員

,但現在我得到的錯誤「網絡路徑找不到」

我怎樣才能解決這個錯誤..感謝

回答

8

我有一個非常類似的問題在第一線更改爲

PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1"); 

看看是否能解決您的問題。並三重檢查程序是否以管理員權限運行。

可能發生的另一個問題是服務器有密碼複雜度要求,並且password正在傳遞給該函數不符合這些要求。如果您通過密碼[email protected]!fda作爲密碼,問題是否消失?

我90%確定它是這兩個問題之一。


對於您的用戶組而不是保存我不知道爲什麼。這是來自我的一個項目的一個小竅門,就是你正在做同樣的事情。我不能看到差異。

using (GroupPrincipal r = GroupPrincipal.FindByIdentity(context, "Remote Desktop Users")) 
using (GroupPrincipal u = GroupPrincipal.FindByIdentity(context, "Users")) 
{ 
    //snip 
    UserPrincipal user = null; 
    try 
    { 
     if (userInfo.NewPassword == null) 
      throw new ArgumentNullException("userInfo.NewPassword", "userInfo.NewPassword was null"); 
     if (userInfo.NewPassword == "") 
      throw new ArgumentOutOfRangeException("userInfo.NewPassword", "userInfo.NewPassword was empty"); 
     //If the user already is in the list of existing users use that one. 
     if (pr.ContainsKey(username)) 
     { 
      user = (UserPrincipal)pr[username]; 
      user.Enabled = true; 
      user.SetPassword(userInfo.NewPassword); 
     } 
     else 
     { 
      //create new windows user. 
      user = new UserPrincipal(context, username, userInfo.NewPassword, true); 
      user.UserCannotChangePassword = true; 
      user.PasswordNeverExpires = true; 
      user.Save(); 
      r.Members.Add(user); 
      r.Save(); 
      u.Members.Add(user); 
      u.Save(); 
     } 
     IADsTSUserEx iad = (IADsTSUserEx)((DirectoryEntry)user.GetUnderlyingObject()).NativeObject; 
     iad.TerminalServicesInitialProgram = GenerateProgramString(infinityInfo); 
     iad.TerminalServicesWorkDirectory = Service.Properties.Settings.Default.StartInPath; 
     iad.ConnectClientDrivesAtLogon = 0; 
     user.Save();    
    } 
    catch(Exception e) 
    { 
     //snip 
    } 
    finally 
    { 
     if (user != null) 
     { 
      user.Dispose(); 
     } 
    } 
} 
+0

如果其中一個密碼問題PasswordExecption將不會拋出一個IOException – 2010-09-16 19:21:31

+0

「網絡路徑找不到」也可以的消息通過COM – 2010-09-16 19:24:56

+0

所以此工程拋出....但是這是不添加用戶組中的用戶....任何幫助? – user175084 2010-09-16 19:26:50