2010-09-16 146 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