Anyone using the new 2.0 version of the Azure AD Graph Client?
I started fooling around with it yesterday but can't get it to work. The GraphConnection
class is marked deprecated and replaced with ActiveDirectoryClient
. In addition all of a sudden it's all Office 365 while I just want to limit my trials to Azure Active Directory without O365. Documentation is hard to find, at least when you don't want to use the O365 and O365 API Tools. The AD samples on GitHub seem to be updated as well but code is still using GraphConnection
class. Go figure.
Not much samples/guidance on using ActiveDirectory client yet so below code using for now
public async Task<ActionResult> Index()
{
List<Exception> exceptions = new List<Exception>();
ProfileViewModel model = new ProfileViewModel();
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(SecurityConfiguration.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(SecurityConfiguration.ClientId, SecurityConfiguration.AppKey);
try
{
var ServiceUri = new Uri(SecurityConfiguration.GraphUrl);
ActiveDirectoryClient client = new ActiveDirectoryClient(ServiceUri, async () =>
{
var result = await authContext.AcquireTokenSilentAsync(SecurityConfiguration.GraphUrl, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return result.AccessToken;
});
try
{
var users = await client.Users.ExecuteAsync();
var user = await client.Users[userObjectID].ExecuteAsync();
}
catch (Exception exc)
{
exceptions.Add(exc);
}
}
catch (AdalSilentTokenAcquisitionException exc)
{
exceptions.Add(exc);
}
ViewBag.Exceptions = exceptions;
return View(model);
}
client.Users.ExecuteAsync()
throws exceptions
The response payload is a not a valid response payload. Please make sure that the top level element is a valid Atom or JSON element or belongs to 'http://schemas.microsoft.com/ado/2007/08/dataservices' namespace.
client.Users[userObjectID].ExecuteAsync()
throws
System.Reflection.TargetInvocationException with Innerexpection Expected a relative URL path without query or fragment. Parameter name: entitySetName
UPDATE 2/11
Spooky resolution: without changing one line of code client.Users.ExecuteAsync()
worked as expected. My thought is that folks at MSFT changed some stuff on the API so that response payload is now correct. They could have mentioned that.
To get user details using v2.0 code below does the trick
var userFetcher = client.Users.Where(u => u.ObjectId == userObjectID);
var user = await userFetcher.ExecuteAsync();
If you are using razor to display content of the user you'll probably get razor exceptions when trying to go through collection like AssignedPlans
The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Resolution is to change compilation settings in your web.config as outlined in http://www.lyalin.com/2014/04/25/the-type-system-object-is-defined-in-an-assembly-that-is-not-reference-mvc-pcl-issue/
<compilation debug="true" targetFramework="4.5" >
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>