使用Azure AD承载令牌根据API应用验证Web应用

使用Azure AD承载令牌根据API应用验证Web应用,azure,oauth,azure-web-app-service,azure-active-directory,bearer-token,Azure,Oauth,Azure Web App Service,Azure Active Directory,Bearer Token,我正在尝试访问我在Azure上托管并受Azure AD保护的API应用程序 对于API应用程序,我已将应用程序服务身份验证设置为Azure Active Directory“Express”管理模式 在“经典”门户中,我在广告下创建了两个应用程序。一个用于API应用程序,另一个用于Web应用程序。对于Web应用程序,我在API应用程序的“其他应用程序的权限”下添加了一个条目(尽管我不确定是否需要此条目,因为API应用程序的“访问应用程序所需的用户分配”处于禁用状态)。我还为Web应用程序生成了一

我正在尝试访问我在Azure上托管并受Azure AD保护的API应用程序

对于API应用程序,我已将应用程序服务身份验证设置为Azure Active Directory“Express”管理模式

在“经典”门户中,我在广告下创建了两个应用程序。一个用于API应用程序,另一个用于Web应用程序。对于Web应用程序,我在API应用程序的“其他应用程序的权限”下添加了一个条目(尽管我不确定是否需要此条目,因为API应用程序的“访问应用程序所需的用户分配”处于禁用状态)。我还为Web应用程序生成了一个密钥

下面是这里给出的示例代码-

我可以使用以下代码成功获取承载令牌:

private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"];

static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

private static string ApiId = ConfigurationManager.AppSettings["ApiId"];

private static AuthenticationContext authContext = new AuthenticationContext(authority);
private static ClientCredential clientCredential = new ClientCredential(clientId, appKey);

这是我用来发出401失败请求的代码:

var apiUri = new Uri(ConfigurationManager.AppSettings["ApiUrl"] + "/api/MethodImCalling");
var client = new RestClient(apiUri.GetLeftPart(UriPartial.Authority));
var request = new RestRequest(apiUri, Method.GET);
request.AddHeader("Authorization", "Bearer " + result.AccessToken);
request.AddParameter("something", somevalue);
var response = client.Execute(request);

if (response.StatusCode != HttpStatusCode.OK)
    return Request.CreateResponse(response.StatusCode); // Relay non-successful response
你知道我可能做错了什么或错过了什么吗?提前谢谢

我已经有Azure中的Logic App访问API应用程序而没有问题,但我注意到Logic App json中的身份验证凭据包含一个“观众”参数。上面的代码没有使用“观众”,所以这可能是拼图中缺少的部分,如果是,我如何添加它

显示如何配置Web应用以访问API应用的屏幕截图:

您得到401响应的原因是您只授予了应用程序委派的权限,但您使用的是需要应用程序权限的客户端凭据流

您可以更改代码以使用授权代码流,也可以将应用程序权限从web应用程序授予web API

要使用授权代码流,您需要将代码改为使用AcquireTokenByAuthorizationCodeAsync

您可以在此处找到有关这两种不同方法的更多信息:

您还可以分享有关如何配置API进行授权的详细信息吗?@PhilippeSignoret-我正在使用应用程序服务Authenization=Azure Active Directory“Express”管理模式(也将添加到问题中)。这就是你想要的细节吗?不确定要提供什么其他细节。干杯。在部署到Azure之前,示例代码在您的本地服务器上运行良好吗?如果访问令牌不允许您访问API,我建议您使用JWT解码工具来分析令牌,这是JWT解码的网页:。@Jambor MSFT-不,很遗憾,代码在本地或Azure上无法工作。干杯。你可以添加一个屏幕截图,说明你如何设置你的Web应用程序的“对oter应用程序的权限”?感谢Saca的提示。我正试图根据您的指示实施一个解决方案。这是一个学习曲线,因为有点让我头疼。一旦我弄明白了,我会把它作为答案。我已经找到了一些微软的代码示例,但我想我会尝试取消“或将应用程序权限从你的web应用程序授予你的web API”选项,因为这听起来像是纯粹的配置。我真的很难做到这一点。我相信这很简单,但我在哪里“将应用程序权限从您的web应用程序授予您的web API”?看起来我的问题可能与这些相同-并且
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Date: Wed, 13 Jul 2016 08:43:09 GMT
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Bearer realm="MY-API-APP-ID-IS-HERE"
  X-Powered-By: ASP.NET
  Content-Length: 58
  Content-Type: text/html
}
var apiUri = new Uri(ConfigurationManager.AppSettings["ApiUrl"] + "/api/MethodImCalling");
var client = new RestClient(apiUri.GetLeftPart(UriPartial.Authority));
var request = new RestRequest(apiUri, Method.GET);
request.AddHeader("Authorization", "Bearer " + result.AccessToken);
request.AddParameter("something", somevalue);
var response = client.Execute(request);

if (response.StatusCode != HttpStatusCode.OK)
    return Request.CreateResponse(response.StatusCode); // Relay non-successful response