如何使用Azure Active Directory授权使用Azure REST API应用程序

我已经为Azure部署了一个API App ,但是如果身份validation(使用AAD)设置为ON,我在创建API客户端时遇到问题。

当我尝试生成服务客户端时(当身份validation为OFF时),然后生成客户端代码(使用Autorest完成)并且代码正在运行,但是当我将身份validation设置为ON时(以及当请求未经过身份validation时要执行的操作设置为Login with Azure Active Directory然后, Login with Azure Active Directory

1)服务呼叫返回401 Unauthorized (无需重定向到AAD登录页面)

2)然后我尝试再次生成服务客户端(从Project的上下文菜单 – >添加 – > REST API客户端 – >然后在我选择的对话框中选择“选择Azure资产”并按下确定并收到消息"Failed to download metadata file for Microsoft Azure API App: ...app name..." (和”没有其他可用信息“)

我根据Azure手册(使用快速设置)实现了AAD:

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-how-to-configure-active-directory-authentication/

正在根据这个video工作,这个video中显示的所有内容都在工作,除了AAD没有被certificate……对我来说它不起作用……

https://azure.microsoft.com/en-us/documentation/videos/connect-2015-what-s-new-in-app-service-api-apps/

有什么建议?

编辑

1)如果我在Web浏览器中输入请求URL(REST API客户端使用) – 然后它返回有效结果2)我发现我使用的是没有凭据的REST API(我认为在这种情况下应该提供Azure AD登录屏幕……但它不是)

编辑2

我得到了一些进展 – 进入AAD登录界面,但输入凭据后我获得了bearer token ,但当我尝试查询服务时,收到错误消息:

AADSTS65005: The client application has requested access to resource 'https....azurewebsites.net'. This request has failed because the client has not specified this resource in its requiredResourceAccess list. Trace ID: 4176e... Correlation ID: 1d612d... Timestamp: 2016-11-13 18:28:34Z

这些是我为实现这一目标而采取的措施:

0)将Microsoft.IdentityModel.Clients.ActiveDirectory nuget pack添加到客户端项目

1)在Azure Active Directory中注册我的客户端应用程序

2)从客户端应用程序调用REST API时,我正在添加ServiceClientCredentials

3)创建ServiceClientCredentials时我提供了4个元素-authority =这是来自AAD App注册 – > Endpoints => Federation MetadataDocumentvērtība(没有起始部分http://login.windows.net/

-resource =>这是REST API uri(=>目标资源的标识符,它是所请求令牌的接收者)

-clientId =>这是我在AAD -redirect Uri =>中注册客户端应用程序后得到的应用程序ID,因为我的客户端应用程序是Native应用程序,那么这只是任何有效的URL

如何在我的客户端应用程序中指定此资源?

client has not specified this resource in its requiredResourceAccess list

我设法找到了如何为Azure REST API App启用AAD授权的解决方案。 万一有人遇到同样的挑战,我希望这会有所帮助。

这些是我做的步骤:

1)在App服务中 – >身份validation/授权

  • App Service Authentication => On
  • 请求未经过身份validation时要采取的操作=>使用AAD登录
  • 使用Express设置配置AAD(您必须为您的API应用程序创建Azure AD应用程序 – 即您的服务的“应用程序注册”)

2)在Azure Active Directory中 – >应用程序注册

  • 为您的客户端应用添加注册
  • 编辑客户端应用程序的清单 – 在requiredResourceAccess部分中,您必须添加有关REST API应用程序的信息:
    • resourceAppId – >在此处插入REST API App id
    • resourceAccess {id} – > REST API的OauthPermission id值(您可以在REST API的清单中获取它!)

3)在您的客户端应用程序中

  • 使用Autorest生成REST客户端(来自解决方案资源管理器: Add\REST API client )或手动创建它
  • 添加Microsoft.IdentityModel.Clients.ActiveDirectory nuget pack
  • 获取并使用令牌访问您的API,代码类似于:

      //request (..) var tokenCreds = getToken(); ServiceClientCredentials credentials = tokenCreds; using (var client = new YourAPI(credentials)) { ... } (..) //getting token private static TokenCredentials getToken() { //get this from Federation Metadata Document in //Azure Active Directory App registrations -> Endpoints var authority = "f1..."; //Identifier of the target resource that is the recipient of the requested token var resource = "https://yourapi.azurewebsites.net"; //client application id (see Azure Active Directory App registration //for your client app var clientId = "a71..."; //return url - not relevant for Native apps (just has to be valid url) var redirectUri = "https://just-some-valid-url.net"; AuthenticationContext authContext = new AuthenticationContext(string.Format ("https://login.windows.net/{0}", authority)); AuthenticationResult tokenAuthResult = authContext.AcquireTokenAsync(resource, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto)).Result; return new TokenCredentials(tokenAuthResult.AccessToken); }