Reputation: 906
I’m writing an MVC application that needs to create users in Azure Active Directory. I’ve wrapped the code that creates the user in its own library and it works fine inside of a unit test. However, when the same code is called inside my MVC Controller the call to AddUserAsync never returns. As a test I changed my controller around so that instead of creating a new user it just tries to get a user. The controller looks like:
public class RegistrationController : BaseController
{
public ActionResult Index()
{
AadAccessHelper adHelper = new AadAccessHelper();
var client = adHelper.AdClient;
AADUser aadUser = adHelper.GetUserByPrincipalName("[email protected]");
return View();
}
}
And the helper class stuff that gets the user looks like:
private async Task<User> GetUserByUserName(ActiveDirectoryClient adClient, string userName)
{
var user = await adClient.Users.Where(u => u.UserPrincipalName == userName).ExecuteSingleAsync();
return (User) user;
}
public AADUser GetUserByPrincipalName(string userName)
{
Task<User> user = GetUserByUserName(AdClient, userName);
user.Wait(new TimeSpan(0, 1, 0));
if (true == user.IsFaulted)
{
throw user.Exception;
}
if (null == user.Result)
{
return null;
}
return MapUserToAADUser(user.Result);
}
The call to ExecuteSingleAsync just never returns no matter how long I give it. What am I missing?
The testing procedure is to run the web-site which comes up in Chrome (I’ve used both IEs too with the same result), I navigate to the registration page (I’ve moved code around for testing purposes) and that is when the sadness begins.
Again: the code works fine when running in a unit test. Do I need to use a different client instead of the Microsoft.Azure.ActiveDirectory.GraphClient.ActiveDirectoryClient?
The purpose of the application when I’m done will be to register new users and allow designated users administrate application users (reset passwords, change user properties, assign users to groups, ect).
I’m in Visual Studio 2015, and whatever the latest stuff is coming out of GitHub.
Any help would be greatly appreciated.
Upvotes: 0
Views: 627
Reputation: 14649
Instead of return the AADUser directly we should use the Task< AADUser>. Here is the code works well for me:
public async Task<AADUser> GetUserByPrincipalName(string userName)
{
var user = await GetUserByUserName(activeDirectoryClient, userName);
return new AADUser()
{
displayName = user.DisplayName
};
}
Controller:
public async Task<ActionResult> test()
{
var user = await new AzureADGraphTest.Users().GetUserByPrincipalName("[email protected]");
return View();
}
Upvotes: 1