c# - 创建本地用户帐户

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

我有这段代码可以创建本地 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% 确定这是这两个问题之一。


对于您的用户组不保存,我不确定为什么。这是我的一个项目的片段,它正在做与您相同的事情。我看不出差异。

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

关于c# - 创建本地用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729406/

相关文章:

在 Task.WhenAll 中执行许多任务时的 C# 线程

c# - PowerShell cmdlet 参数验证

c# - 想要在 GridView 中绑定(bind)对象内的对象。这可能吗?

asp.net - .NET 中的 Tcp/Ip 套接字连接

windows - SetWindowPos 如何影响所有者窗口的 Z 顺序?

c# - 在 Windows 应用程序中打开弹出窗口时是否可以制作阴影(禁用)类图像?

c# - 远程处理 - 有关客户端的信息。 C#

c# - 泛型是否在性能和优缺点方面给我带来了一些优势?

c# - 为什么我在 azure 应用程序服务中从表单发布时收到 404 错误,但它在 localhost 中运行良好

windows - matlab低优先级系统调用