locked
Unable to create user on windows server 2008 due to access permission RRS feed

  • Question

  • 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());
                }
            }
    When i change the IP from 127.0.0.1 to the particular IP of the machine, then code gets executed without any exception but its not creating user.


    --
    Thanks,
    Jitendra

    Friday, August 17, 2012 10:26 AM