【问题标题】:create local user account创建本地用户帐户
【发布时间】:2011-04-13 08:46:18
【问题描述】:

我有这个代码来创建本地 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 服务器上。并试图在那里创建一个用户。

首先我收到错误“一般访问被拒绝错误” 所以我让用户成为管理员

但现在我收到错误“找不到网络路径”

我该如何解决这个错误..谢谢

【问题讨论】:

    标签: c# asp.net windows windows-server-2008 user-accounts


    【解决方案1】:

    我有一个非常相似的问题,将第一行更改为

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

    看看这是否能解决您的问题。并三重检查程序是否以管理员权限运行。

    另一个问题可能是服务器有密码复杂性要求,而传递给函数的password 不满足这些要求。如果您将ASfas123@!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();
            }
        }
    }
    

    【讨论】:

    • 如果出现密码问题,会抛出 PasswordExecption 而不是 IOException
    • “找不到网络路径”也可以是COM抛出的消息
    • 所以这行得通....但这不是在用户组中添加用户....有什么帮助吗??
    • 我看不出你在做什么和我在做什么之间有任何区别。也许在 group.save() 之后尝试 user.save() 但我怀疑这是原因。
    • 事实上我不得不使用user175084提出的原始解决方案:PrincipalContext context = new PrincipalContext(ContextType.Machine);
    猜你喜欢
    • 2013-02-16
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多