MVC Model for adding/editing/removing active directory user

0

The following code:

public string AddADUser(string user_name, string password, string first_name, string last_name, string company_name) {
    try {
        List<ADUser> ADUsers = new List<ADUser>();
        string admin_userName = "Administrator";
        string admin_password = "Password!";
        string domain = "sneaky";
        var context = new PrincipalContext(ContextType.Domain, domain, admin_userName, admin_password);


        UserPrincipal NewUserPrincipal = new UserPrincipal(context, user_name, password, true);

        NewUserPrincipal.UserPrincipalName = user_name;
        NewUserPrincipal.SamAccountName = user_name;
        // company NewUserPrincipal.GetUnderlyingObject.

        NewUserPrincipal.GivenName = first_name;
        NewUserPrincipal.Surname = last_name;
        NewUserPrincipal.DisplayName = user_name;
        NewUserPrincipal.Enabled = true;

        NewUserPrincipal.Save();
        return "User Saved Sucessfully";
    } catch (Exception ex) {
        return "Error saving user: \n" + ex.ToString();
    }
}

Produces the following error:

Error saving user: System.DirectoryServices.AccountManagement.PrincipalOperationException: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) ---> System.Runtime.InteropServices.COMException: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) --- End of inner exception stack trace --- at System.DirectoryServices.AccountManagement.ADStoreCtx.Insert(Principal p) at System.DirectoryServices.AccountManagement.Principal.Save() at project.Models.UserBAL.AddADUser(String user_name, String password, String first_name, String last_name, String company_name) in C:\Users\sneakyguy\Desktop\project\project\Models\UserBAL.cs:line 107

The wierd thing is that I was getting an access denied error for another user, I thought maybe he didn't have permission to create user... so I tried admin account and now I'm getting this error. I know eventually I need to make it so that the user performing the action must have the rights to create/edit and delete users in the active directory... and I need to know how to setup those rights in ad too. I also need to set the company field for the user too.

c#
asp.net-mvc
active-directory
asked on Stack Overflow Jan 8, 2017 by gunslingor • edited Jan 8, 2017 by gunslingor

1 Answer

0

The RPC server is unavailable error message suggests your VS development machine is having difficulty contacting your domain controller to add the user.

answered on Stack Overflow Jan 8, 2017 by MMS_guru

User contributions licensed under CC BY-SA 3.0