Question

So I am working with the default MVC 5 out of box template tied to Azure AD. It worked fine when last run, but now I cannot get the user profile to run. Something that had worked fine before. I tested a second project that I knew to work, and am now receiving the same error. If you look at the code in the default template it looks like this.

  public async Task<ActionResult> UserProfile()
     {
        string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;

        // Get a token for calling the Windows Azure Active Directory Graph
        AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, LoginUrl, tenantId));
        ClientCredential credential = new ClientCredential(AppPrincipalId, AppKey);
        AuthenticationResult assertionCredential = authContext.AcquireToken(GraphUrl, credential);

        string authHeader = assertionCredential.CreateAuthorizationHeader();
        string requestUrl = String.Format(
            CultureInfo.InvariantCulture,
            GraphUserUrl,
            HttpUtility.UrlEncode(tenantId),
            HttpUtility.UrlEncode(User.Identity.Name));

        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
        request.Headers.TryAddWithoutValidation("Authorization", authHeader);
        HttpResponseMessage response = await client.SendAsync(request);
        string responseString = await response.Content.ReadAsStringAsync();
        UserProfile profile = JsonConvert.DeserializeObject<UserProfile>(responseString);

        return View(profile);
    }

It is specifically failing on this line:

     AuthenticationResult assertionCredential = authContext.AcquireToken(GraphUrl, credential);

I have checked my Azure portal and all endpoints are correct. Nothing in that regard has changed. The stack trace isn't giving me anything meaningful. Other than the HTTP 400 Error on the web page, and inside the Chrome dev tools I see an HTTP 500 Server error. Everything seems to be working other than this. Not sure if there are changes to Azure that have happened in the last 4 days. But I cannot find any information on this issue.

I am using VS 2013. Not a lot of documentation yet on this version, as the AD Graph API seems to be setup different than they show in the examples. Any help in how to debug this further would be greatly appreciated.

Was it helpful?

Solution

Try going into your nuget package manager and updating the Active Directory Authentication Library.

I was banging my head against the wall with this problem until I realized I was on the 1.0 version of this library, the current is 2.1.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top