Question

I'm attempting to secure an ASP.NET web API using the OWIN providers for Windows Azure Active Directory. I've configured the web api to use Windows Azure JWT bearer tokens as per below:

public void Configuration(IAppBuilder app)
{
   app.UseWindowsAzureBearerToken(new WindowsAzureJwtBearerAuthenticationOptions()
     {
       Audience = "http://mywebsitename.azurewebsites.net",
       Tenant = "mydefaultdirectory.onmicrosoft.com"
     });
}

I then created a client application belonging to the same directory as the web api tenant. I specified the permissions to grant the client application access to the web api. The problem occurred when I tried to create a client application and authenticate against the API.

The client is just a simple windows store app with a button click method to authenticate against windows azure, and the code for the click even is below:

 private async void myButton_Click(object sender, RoutedEventArgs e)
    {

        AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/nameofazureactivedirectorytenant");
        AuthenticationResult ar =
         await ac.AcquireTokenAsync("api/pathtoresource",
          "xxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxx");
        // Call Web API
        string authHeader = ar.CreateAuthorizationHeader();
        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(
          HttpMethod.Get, "http://mywebsitename.azurewebsites.net/api/pathtoresource");
        request.Headers.TryAddWithoutValidation("Authorization", authHeader);
        HttpResponseMessage response = await client.SendAsync(request);
        string responseString = await response.Content.ReadAsStringAsync();
    }

The app is failing to correctly create the authentication context, so I believe I am passing it the wrong parameter. I have been through a number of blog posts and SO answers, but nothing has worked thus far, any help would be appreciated!

Was it helpful?

Solution

Have a look at the samples available at https://github.com/AzureADSamples. I think there are some examples there that should be applicable.

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