active-directory-dotnet-graphapi-console icon indicating copy to clipboard operation
active-directory-dotnet-graphapi-console copied to clipboard

ActiveDirectoryClient.Users.AddUserAsync(newAzureUser).Wait(); - Hang

Open Carl123001 opened this issue 10 years ago • 11 comments

I am trying to run this code (below) and it never completes the job. I am authorized to create a user and I tested my authorization by receiving all the users in my active directory using the same Active Directory Client.

        User newAzureUser = new User();
        newAzureUser.GivenName = "John";
        newAzureUser.Surname = "Smith";
        newAzureUser.UserPrincipalName = "John" + "." + "Smith"+ "@" + tenantName;
        newAzureUser.DisplayName = "John";
        newAzureUser.AccountEnabled = true;

        newAzureUser.PasswordProfile = new PasswordProfile
        {
            Password = "Test!354",
            ForceChangePasswordNextLogin = false,
        };

        try
        {
            await ActiveDirectoryClient.Users.AddUserAsync(newAzureUser);
        }
        catch (Exception e)
        {
            Console.WriteLine("\nError creating new user {0} {1}", e.Message,
                e.InnerException != null ? e.InnerException.Message : "");
        }

Carl123001 avatar Jan 29 '15 18:01 Carl123001

were you ever able to create a user successfully?

rhythmnewt avatar Mar 31 '15 00:03 rhythmnewt

yes.. but now i cannot update user-manager with it.

On Tue, Mar 31, 2015 at 2:15 AM, dimkanewtown [email protected] wrote:

were you ever able to create a user successfully?

— Reply to this email directly or view it on GitHub https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet/issues/11#issuecomment-87882125 .

-bElaie.

belaie avatar Mar 31 '15 05:03 belaie

For me both creating and saving a user work successfully now. I haven't tried adding a manager to a user though, but will be doing groups.

Assume

IUser azureUser = null;

Creating user:

                    azureUser = new AzureUser
                    {
                        GivenName = model.FirstName,
                        Surname = model.LastName,
                        DisplayName = model.FirstName + " " + model.LastName,
                        UserPrincipalName = target.ExternalId,
                        AccountEnabled = true,
                        MailNickname = "mail",
                        PasswordProfile = new PasswordProfile
                        {
                            Password = tempPassword,
                            ForceChangePasswordNextLogin = true
                        },
                        UsageLocation = "US",
                        OtherMails = new []{model.Email}
                    };
                    await client.Users.AddUserAsync(azureUser);

Retrieving and editing user:

                    var retrievedUsers = client.Users.Where(u => u.UserPrincipalName.Equals(target.ExternalId))
                            .ExecuteAsync()
                            .Result.CurrentPage;

                    azureUser = retrievedUsers.First();
                    azureUser.DisplayName = model.FirstName + " " + model.LastName;
                    azureUser.GivenName = model.FirstName;
                    azureUser.Surname = model.LastName;
                    azureUser.UserPrincipalName = target.ExternalId;
                    azureUser.OtherMails[0] = model.Email; 
                    await azureUser.UpdateAsync();

rhythmnewt avatar Apr 02 '15 15:04 rhythmnewt

​Yes, i made it work, however I could not update the user-manager relationship anyhow.. but I was wondering how to get user SubscribedSku details using Graph API?.

In the console app code sample there is a code sample which shows how to get the SubscribedSku and SerivicePlanInfo info associated with tenant but not to specific user.​

On Thu, Apr 2, 2015 at 5:05 PM, dimkanewtown [email protected] wrote:

For me both creating and saving a user work successfully now. I haven't tried adding a manager to a user though, but will be doing groups.

Assume

IUser azureUser = null;

Creating user:

                azureUser = new AzureUser
                {
                    GivenName = model.FirstName,
                    Surname = model.LastName,
                    DisplayName = model.FirstName + " " + model.LastName,
                    UserPrincipalName = target.ExternalId,
                    AccountEnabled = true,
                    MailNickname = "mail",
                    PasswordProfile = new PasswordProfile
                    {
                        Password = tempPassword,
                        ForceChangePasswordNextLogin = true
                    },
                    UsageLocation = "US",
                    OtherMails = new []{model.Email}
                };
                await client.Users.AddUserAsync(azureUser);

Retrieving and editing user:

                var retrievedUsers = client.Users.Where(u => u.UserPrincipalName.Equals(target.ExternalId))
                        .ExecuteAsync()
                        .Result.CurrentPage;

                azureUser = retrievedUsers.First();
                azureUser.DisplayName = model.FirstName + " " + model.LastName;
                azureUser.GivenName = model.FirstName;
                azureUser.Surname = model.LastName;
                azureUser.UserPrincipalName = target.ExternalId;
                azureUser.OtherMails[0] = model.Email;
                await azureUser.UpdateAsync();

— Reply to this email directly or view it on GitHub https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet/issues/11#issuecomment-88938485 .

-bElaie.

belaie avatar Apr 05 '15 09:04 belaie

Hi Belaie,

How did you make it work? I'm experiencing the same ActiveDirectoryClient issue now and don't understand how it could be caused.

jyuan08 avatar Jun 30 '15 16:06 jyuan08

I also cannot create users for some reason. I can read users from Active Directory and I can update them, but I can't create them (exception message is "An error occurred while processing this request") and when I try to delete users it complains about my permissions even though I enabled all permissions (and every combination thereafter) under my app's configurations. Does anyone have any idea what the problem could be?

tallen11 avatar Aug 04 '15 21:08 tallen11

See https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet/issues/5 Unfortunately we don't have any app-only permissions exposed that will allow user deletion. I created another issue to track updating the instructions/readme. Also I might refactor the application to perform the deletion operations in the context of the signed in user (delegated permission) - you'll still need to run as an admin.

dkershaw10 avatar Aug 05 '15 01:08 dkershaw10

Ok, thanks for the quick reply. As far as adding users goes, do you have any idea what could be going wrong? Are there any debug messages that would be helpful in diagnosing the problem?

Here's my code:

if (defaultDomain.Name != null) {
                IUser newUser = new User();
                newUser.GivenName = data.firstName;
                newUser.Surname = data.lastName;
                newUser.DisplayName = data.firstName + " " + data.lastName;
                newUser.UserPrincipalName = data.firstName.ToLower() + data.lastName.ToLower() + "@" + defaultDomain.Name;
                newUser.JobTitle = data.jobTitle;
                newUser.Department = data.businessUnitName;
                newUser.CompanyName = "Company";
                newUser.UsageLocation = "US";
                newUser.MailNickname = "mail";
                newUser.AccountEnabled = true;
                newUser.PasswordProfile = new PasswordProfile {
                    Password = "!Password123",
                    ForceChangePasswordNextLogin = true
                };

                try {
                    client.Users.AddUserAsync(newUser).Wait();
                    Console.WriteLine("Added User: ", newUser.DisplayName);
                } catch (AggregateException e) {
                    Console.WriteLine("Error Creating User: " + e.InnerException.Message);
                }
            }

Again, the exception message thrown when trying to call AddUserAsync is "An error occurred while processing this request"

Thanks for the help

tallen11 avatar Aug 05 '15 12:08 tallen11

I finally got it to work. Turns out setting the CompanyName property was causing the creation to fail.

tallen11 avatar Aug 11 '15 14:08 tallen11

I am having this issue where the AddUserAsync(IUser) task never completes. The users that I am creating have bare minimal properties. If I do:

ActiveDirectoryClient.Users.AddUserAsync(newUser).Wait();

It never completes. However, with the same user object, I do:

ActiveDirectoryClient.Users.AddUserAsync(newUser).Wait(TimeSpan.FromSeconds(5));

It does complete successfully, but only after about 5001 milliseconds. In both cases, I can see the Graph API call going and completing successfully using Fiddler.

Here is a full example,

IUser newUser = new User();
newUser.UserPrincipalName = username + "@" + domain; // default domain
newUser.DisplayName = "John Doe";
newUser.AccountEnabled = true;
newUser.MailNickname = newUser.UserName;
newUser.PasswordProfile = new PasswordProfile { Password = password, ForceChangePasswordNextLogin = false };
newUser.UsageLocation = "CA"; // Canada
ActiveDirectoryClient.Users.AddUserAsync(newUser).Wait(); // never returns

pbolduc avatar Sep 29 '15 23:09 pbolduc

I bet my issue is related to http://stackoverflow.com/questions/25172135/task-wait-locks-the-flow-of-the-controller-in-mvc5/25172951#25172951 I am going to refactor my code and ensure I have covered this off.

Confirmed: do not Wait() from within a MVC / Web APi thread.

pbolduc avatar Oct 01 '15 18:10 pbolduc