创建本地用户帐户

我有这个代码来创建一个本地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服务器上。并试图在那边创建一个用户。 首先我得到错误“一般访问被拒绝错误” 所以我让用户成为管理员 但现在我收到错误“找不到网络路径” 我怎么能解决这个错误..谢谢     
已邀请:
我有一个非常类似的问题,改变了第一行
PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1");
看看是否能解决您的问题。并使用管理员权限检查程序是否正在运行。 另一个问题可能是服务器有密码复杂性要求,并且传入函数的
password
不符合这些要求。如果你把它作为密码传递
ASfas123@!fda
,问题是否会消失? 我90%肯定这是这两个问题之一。 对于不保存的用户组,我不确定原因。这是我的一个项目中的一个snippit,它正在做同样的事情。我看不出这种差异。
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();
        }
    }
}
    
检查您是否启用了UAC,或者您必须编写代码以提升您的应用程序权限。然而,这会重新启动您的应用程 http://support.microsoft.com/kb/981778     

要回复问题请先登录注册