0

I am getting below error while trying to create user on remote computer.

System.UnauthorizedAccessException: General access denied error at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() at System.DirectoryServices.DirectoryEntry.RefreshCache() at System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() at System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) at System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() at System.DirectoryServices.AccountManagement.Principal.set_DisplayName(String value) at testemail.CreateLocalWindowsAccount(String username, String password, String displayName, String description, Boolean canChangePwd, Boolean pwdExpires)

here is the code.

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());
        }
    }
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
  • 1
    Are you running this application under an account that has sufficient privileges to create accounts on the remote server? If not then you will have to grant sufficient permissions to the account that you are running your application under. – Darin Dimitrov Aug 15 '12 at 14:40
  • I am running this code in a website. That website is hosted on windows server 2008. – Jitendra Pancholi Aug 15 '12 at 15:13
  • But this doesn't answer my question: have you configured your website to run under an account that has sufficient privileges? Apparently you haven't thus the exception you are getting. – Darin Dimitrov Aug 15 '12 at 15:14
  • yes, website is running under administrator account. – Jitendra Pancholi Aug 15 '12 at 15:15
  • But is this a local administrator account? It looks like you are trying to create accounts on some other machine. Does this account has sufficient privileges on this remote machine? – Darin Dimitrov Aug 15 '12 at 15:16
  • Yes, its local administrator account and i am trying to create a user account on the same machine thats why i wrote 127.0.0.1( means local machine). Is it incorrect? – Jitendra Pancholi Aug 15 '12 at 15:17
  • 1
    After changing the local IP(127.0.0.1) to that particular IP of the machine, the code gets executed without any exception or error but its not creating the user under specified user group. – Jitendra Pancholi Aug 17 '12 at 07:39
  • @DarinDimitrov: Have you checked my code after above comment? – Jitendra Pancholi Aug 17 '12 at 09:50

0 Answers0