【发布时间】:2014-10-29 19:37:42
【问题描述】:
我在 Microsoft Azure 上有两个不同的 Web 项目。一个项目是 .NET MVC Web 应用程序,另一个项目是 .NET Web API。
这两个项目都配置为使用 Azure AD。 MVC Web 应用程序能够获取令牌并使用它向 Web API 发出请求。这是来自 MVC Web 应用程序的示例代码。
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
result = authContext.AcquireTokenSilent(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
// Make a call against the Web Api
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, webApiBaseAddress + "/api/list");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
所以这段代码工作得很好。但是,我现在需要做的是直接从 AngularJS 应用程序调用 Web API。当我尝试这样做时,我收到 401 未经授权的错误。
我这样做的方法是在 AngularJS 发送的 HTTP GET 请求中添加一个标头。我将“Bearer”设置为我从 MVC 应用程序(上面的代码)传递给页面的结果。AccessToken 值。
显然这不起作用。我想现在我的问题是我的选择是什么?有没有官方或更好的方法来做到这一点?假设我想从标准 JavaScript 调用 Web API(让我们忘记 AngularJS 的复杂性)。有没有办法通过 Azure AD 进行身份验证?
【问题讨论】:
标签: asp.net asp.net-mvc angularjs asp.net-mvc-4 azure