【问题标题】:How to add simple authentication to azure mobile/web apps with Azure Active Directory?如何使用 Azure Active Directory 向 Azure 移动/Web 应用程序添加简单的身份验证?
【发布时间】:2016-01-19 06:26:44
【问题描述】:

我的目标是保护我的移动应用自定义 API 方法,然后通过 httpclient(c#) 使用。

作为测试的一部分,我使用 Postman 请求令牌并使用它来访问资源。

【问题讨论】:

  • 请注意,即使是自我回答的问题也应符合网站的质量要求;见How to Ask“如何” 问题不适合 SO,请考虑将其写在博客或其他地方。
  • 感谢您的建议。我会记下来的。

标签: azure azure-web-app-service azure-active-directory azure-mobile-services


【解决方案1】:

我将用创建新移动应用时附带的 vanilla 模板进行说明。

  1. 创建一个新的移动应用程序。

  2. 将其发布到 Azure。

  3. 在 Azure 门户上打开它,转到 设置 边栏选项卡。查找身份验证/授权

  4. 应用服务身份验证设为开启,然后选择Azure Active Directory>Express>创建新的AD应用

  5. 打开 Active Directory(管理门户),为您的帐户选择目录。

  6. 选择您刚刚创建的应用程序。

  7. 转到配置选项卡并复制ClientId,创建密钥,也复制它。

  8. 点击底部的View EndPoints,复制OAuth 2.0 Token EndpointOauth2.0 Token

  9. 现在打开移动应用项目,并使用 [Authorize] 装饰您想要项目的控制器/方法。

你应该已经设置好了。

现在打开你最喜欢的客户端,在我的例子中是 Postman 和

第 1 步:请求令牌

Method: POST
URL : {Oauth TokenEndPoint from Step. 8}
grant_type : client_credentials
client_id : {one copied from AD section in Step. 7}
client_secret : {one copied from AD section in Step. 7}
resource : {one copied from AD section in Step. 7}

你会收到这样的回复

  "token_type": "Bearer",
  "expires_in": "3600",
  "expires_on": "1453151213",
  "not_before": "1453147313",
  "resource": "yyyyyyyyyyyyyyyyyy",
  "access_token": "xxxxxxxxxxxxxxxxxxx"

现在复制 access_token 并在您对移动应用的请求中使用它。

   Method: Get
    URL : https://MyMobileApp.azurewebsites.net/api/values?ZUMO-API-VERSION=2.0.0
   Headers : Authorization : Bearer xxxxxxxxxxxxxxxxxxx

如果您遇到任何问题,这里是关键步骤。转到 Azure 门户并打开应用程序日志记录、详细错误消息、诊断日志下的失败请求跟踪(设置刀片)。

现在您可以在 Log Stream(Tools Blade) 下查看正在发生的事情和更详细的日志记录。

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    相关资源
    最近更新 更多