1
我正在嘗試使用Windows Azure Active Directory的OWIN提供程序來保護ASP.NET Web API。我已經配置了Web API來使用Windows Azure的智威湯遜承載令牌按如下:如何使用OWIN/Katana來保護ASP.NET MVC Web Api?
public void Configuration(IAppBuilder app)
{
app.UseWindowsAzureBearerToken(new WindowsAzureJwtBearerAuthenticationOptions()
{
Audience = "http://mywebsitename.azurewebsites.net",
Tenant = "mydefaultdirectory.onmicrosoft.com"
});
}
我然後創建屬於同一目錄網頁API租戶的客戶端應用。我指定了授予客戶端應用程序訪問Web API的權限。當我嘗試創建一個客戶端應用程序並根據API進行身份驗證時,發生了問題。
客戶端只是一個簡單的Windows應用商店的應用程序通過單擊按鈕的方法來對Windows Azure的身份驗證,併爲點擊代碼甚至低於:
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();
}
該應用程序未能正確地創建認證上下文,所以我相信我傳遞了錯誤的參數。我已經通過一些博客文章和SO答案,但迄今爲止沒有任何工作,任何幫助將不勝感激!