12

ローカル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サーバーに置きました。あそこにユーザーを作ろうとしました。

最初に「一般アクセス拒否エラー」というエラーが発生したので、ユーザーを管理者にしました

しかし今、私は「ネットワークパスが見つかりませんでした」というエラーを受け取ります

どうすればこのエラーを解決できますか..ありがとう

4

1 に答える 1

10

非常によく似た問題があり、最初の行を次のように変更しました

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

問題が解決するかどうかを確認してください。そして、プログラムが管理者権限で実行されていることをトリプルチェックしてください。

考えられるもう1つの問題は、サーバーにパスワードの複雑さの要件がありpassword、関数に渡されていることがそれらの要件を満たしていないことです。ASfas123@!fdaパスワードとして渡すと問題は解決しますか?

私はそれがこれら2つの問題の1つであると90%確信しています.


ユーザーグループが保存されていないため、理由がわかりません。これは、あなたと同じことをしている私のプロジェクトの 1 つからの抜粋です。違いがわかりません。

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();
        }
    }
}
于 2010-09-16T18:21:20.673 に答える