0

尝试在远程计算机上创建用户时出现以下错误。

System.UnauthorizedAccessException:在 System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() 在 System.DirectoryServices.DirectoryEntry.RefreshCache() 的 System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() 的一般访问被拒绝错误 System.DirectoryServices.AccountManagement .PrincipalContext.Initialize() 在 System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) 在 System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() 在 System.DirectoryServices.AccountManagement.Principal.set_DisplayName(String value) 在 testemail.CreateLocalWindowsAccount (字符串用户名、字符串密码、字符串 displayName、字符串描述、布尔 canChangePwd、布尔 pwdExpires)

这是代码。

public void CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
    {
        try
        {
            PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1");
            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, "Remote Desktop Users");
            group.Members.Add(user);
            group.Save();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
4

0 回答 0