[英]How do I secure an ASP.NET MVC Web Api using OWIN/Katana?
我正在嘗試使用Windows Azure Active Directory的OWIN提供程序來保護ASP.NET Web API。 我已將Web api配置為使用Windows Azure JWT承載令牌,如下所示:
public void Configuration(IAppBuilder app)
{
app.UseWindowsAzureBearerToken(new WindowsAzureJwtBearerAuthenticationOptions()
{
Audience = "http://mywebsitename.azurewebsites.net",
Tenant = "mydefaultdirectory.onmicrosoft.com"
});
}
然后,我創建了一個客戶端應用程序,該應用程序與Web api租戶屬於同一目錄。 我指定了權限,以授予客戶端應用程序對Web api的訪問權限。 當我嘗試創建客戶端應用程序並對API進行身份驗證時,發生了問題。
客戶端只是一個簡單的Windows應用商店,具有按鈕click方法來針對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();
}
該應用無法正確創建身份驗證上下文,因此我相信我將其傳遞給了錯誤的參數。 我已經瀏覽了許多博客文章和答案,但是到目前為止沒有任何工作,任何幫助將不勝感激!
看一下https://github.com/AzureADSamples上可用的示例。 我認為那里有一些例子應該適用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.