【发布时间】:2025-12-18 05:30:01
【问题描述】:
我创建了一个“测试”项目,我在其中使用一个 .Net 4.6 WebApi,我想使用 ADFS 集成身份验证 - 类似于 this post。我从 Angular 项目中调用 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 登录页面(我不确定为什么会发生这种情况任何一个)。
【问题讨论】:
标签: c# asp.net-web-api oauth-2.0 adfs