尝试添加 Windows 用户帐户时访问被拒绝

发布于 2024-11-19 21:33:59 字数 1473 浏览 9 评论 0原文

我正在尝试以编程方式添加如下所示的用户,但在保存时收到访问被拒绝的消息。我在 Windows 7 上本地运行,代码驻留在控制台应用程序中。

/// <summary>
/// 
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="description"></param>
public static void CreateUser(string userName, string password, string description)
{
    PrincipalContext pc = new PrincipalContext(ContextType.Machine, null);
    System.DirectoryServices.AccountManagement.UserPrincipal u = new UserPrincipal(pc);
    u.SetPassword(password);
    u.Name = userName;
    u.Description = description;
    u.UserCannotChangePassword = true;
    u.PasswordNeverExpires = true;
    u.Save();

    GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Users");
    gp.Members.Add(u);
    gp.Save();
}

有什么想法吗?我尝试提供管理员用户名和密码,但仍然遇到相同的错误。

控制台应用程序的执行方式如下:

 ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.UserName = userName;
                    startInfo.Password = securePassword;
                    startInfo.LoadUserProfile = true;
                    startInfo.UseShellExecute = false;
                    startInfo.FileName = batchPath;
                    startInfo.Arguments = operationLogID.ToString();
                    Process.Start(startInfo);

以下是代码设置方式的粗略视图:

  • 控制台应用程序测试工具在调试模式下执行。
  • 我检查用户,如果他们不存在..然后我尝试创建如上所示的用户。这就是错误发生的地方。

I am trying to programmatically add a user like this below but get an access denied message on the Save. I'm running locally on Windows 7 and the code resides in a console app.

/// <summary>
/// 
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="description"></param>
public static void CreateUser(string userName, string password, string description)
{
    PrincipalContext pc = new PrincipalContext(ContextType.Machine, null);
    System.DirectoryServices.AccountManagement.UserPrincipal u = new UserPrincipal(pc);
    u.SetPassword(password);
    u.Name = userName;
    u.Description = description;
    u.UserCannotChangePassword = true;
    u.PasswordNeverExpires = true;
    u.Save();

    GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Users");
    gp.Members.Add(u);
    gp.Save();
}

Any ideas? I tried supplying an administrators username and password and still get the same error.

The console app gets executed like this:

 ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.UserName = userName;
                    startInfo.Password = securePassword;
                    startInfo.LoadUserProfile = true;
                    startInfo.UseShellExecute = false;
                    startInfo.FileName = batchPath;
                    startInfo.Arguments = operationLogID.ToString();
                    Process.Start(startInfo);

Here is a rough view of how the code is set up:

  • Console App test harness gets executed in debug mode.
  • I check for a user and if they don't exist..then I try and create it shown above. This is where the error occurs.

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

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

发布评论

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

评论(1

情话墙 2024-11-26 21:34:00

即使您以管理员身份登录,也需要以管理员身份运行控制台。以下是如何以管理员身份启动控制台:http://www.howtogeek .com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/

然后找到您的控制台应用程序并运行它。

祝你好运!

-迈克尔

Even if you're logged in as admin, you need to run your console as admin. Here's how to launch a console as admin: http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/.

Then find your console app and run it.

Good luck!

-Michael

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