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.

1 Answer 1

0

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

3
  • Yeah. I tried the other user/pass that previously gave an access denied error and now its giving the same RPC error... I don't know what changed, I changed the user and pass and then I started getting this error. I know I can ping the server. Also, the other area of code that is getting the users of the system is working, so it is communicating to AD... it's only producing this error when I try to set a new user. I'm not sure if the code is correct.
    – gunslingor
    Jan 8, 2017 at 22:14
  • I added the AD svr in the visual studio server explorer... but it's still producing the same error.
    – gunslingor
    Jan 8, 2017 at 22:23
  • Turned off the AD firewall, no change.
    – gunslingor
    Jan 8, 2017 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.