创建本地用户帐户

发布于 2024-09-24 02:24:43 字数 1233 浏览 3 评论 0原文

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

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

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

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

i have this code to create a local windows user

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;
        }

    }

i tried this on my machine it works fine. but then i put this on windows server. and tried to create a user over there.

First i got the error "General access denied error"
so i made the user an administrator

but now i get the error "The network path was not found"

how can i solve this error.. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2024-10-01 02:24:44

我有一个非常类似的问题,更改第一行,

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

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

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

I had a very similar issue change the first line to

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

see if that fixes your issue. And triple check that the program is running with administrator privileges.

The other issue it could be is the server has password complexity requirements and password that is being passed in to the function does not meet those requirements. Does the problem go away if you pass it ASfas123@!fda as the password?

I am 90% sure it is one of those two issues.


For your user groups not saving I am not sure why. Here is a snippit from one of my projects that is doing the same thing you are. I cant see the diffrence.

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文