[英]WebApi and ADFS integration
我創建了一個“測試”項目,我正在使用.Net 4.6 WebApi,我希望使用ADFS集成身份驗證 - 類似於這篇文章 。 我從一個角度項目調用api並使用以下代碼我可以獲得授權標題:
string authority = ConfigurationManager.AppSettings["adfsEndpoint"].ToString();
string resourceURI = "https://localhost:44388/";
string clientID = "someguid";
string clientReturnURI = "http://localhost:55695/";
var ac = new AuthenticationContext(authority, false);
//This seems to be working as I am getting a token back after successful authentication
var ar = await ac.AcquireTokenAsync(resourceURI, clientID, new Uri(clientReturnURI), new PlatformParameters(PromptBehavior.Auto));
string authHeader = ar.CreateAuthorizationHeader();
//this fails with a 401
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:64038/api/Values");
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
var response = await client.SendAsync(request);
return response ;
但是,在隨后調用我正在使用Authorize屬性的ValuesController時,我總是收到401 Unathorized響應(即使我傳遞了Authorization標頭)。 我不確定我錯過了什么。
還有一點需要注意:當我被提示輸入我的憑據時,我得到下面的對話框,而不是我使用ADFS進行身份驗證的普通MVC應用程序獲得的典型ADFS登錄頁面(我不知道為什么會發生這種情況)。
啊! 事實證明我錯過了ConfigureAuth方法中需要的這段代碼:
app.UseActiveDirectoryFederationServicesBearerAuthentication(
new ActiveDirectoryFederationServicesBearerAuthenticationOptions
{
Audience = ConfigurationManager.AppSettings["ida:Audience"],
MetadataEndpoint = ConfigurationManager.AppSettings["ida:MetadataEndpoint"]
});
一旦我添加了這個並在web.config文件中進行了必要的配置(並糾正了傳遞給AcquireTokenAsync方法的resourceUri變量),我就可以從我的api控制器進行一次http調用到使用Authorize修飾的值控制器使用教程中的以下代碼進行屬性:
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:64038/api/Values");
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
var response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
return responseString;
這仍然不適用於AngularJS客戶端(我現在理解),所以我將為此實現ADAL JS庫。
編輯
事實證明,根據這個答案,我似乎無法做我希望做的事情(使用On-Premise ADFS的WebApi后端的AngularJS應用程序)。 我決定使用MVC-AngularJS方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.