簡體   English   中英

創建本地用戶帳戶

[英]create local user account

我有這個代碼來創建一個本地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%肯定這是這兩個問題之一。


對於不保存的用戶組,我不確定原因。 這是我的一個項目中的一個snippit,它正在做同樣的事情。 我看不出這種差異。

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

檢查您是否啟用了UAC,或者您必須編寫代碼以提升您的應用程序權限。 然而,這會重新啟動您的應用程

http://support.microsoft.com/kb/981778

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM