如何使用 Azure Active Directory 向 azure 移动/Web 应用程序添加简单的身份验证? [英] How to add simple authentication to azure mobile/web apps with Azure Active Directory?

查看:25
本文介绍了如何使用 Azure Active Directory 向 azure 移动/Web 应用程序添加简单的身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to secure my mobile app custom API methods and use then via httpclient(c#).

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

As a part of the testing I used Postman to request a token and use it to access the resource.

推荐答案

我会用新建移动应用时自带的vanilla模板来说明.

I will explain with the vanilla template that comes when creating a new Mobile App.

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

  1. Create a new mobile app.

将其发布到 Azure.

Publish it to Azure.

在 Azure 门户上打开它,转到 设置 刀片.查找身份验证/授权.

Open it on Azure portal, Go to Settings blade. Find Authentication / Authorization.

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

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

Open Active Directory (Management Portal), pick the directory for your account.

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

Select Applications, the one you just created.

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

Go to Configure tab and copy the ClientId, Create a Key, copy it too.

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

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

Now open the mobile app project and decorate the controller/method you want to project with [Authorize].

你应该已经设置好了.

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

Now open your favorite client, in my case Postman and

第 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 并在您对移动应用的请求中使用它.

Now copy the access_token and use it in your request to the mobile app.

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

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

In case if you run into any issues, here is a key step. Go to Azure portal and turn on Application Logging, Detailed error messages, Failed request tracking under Diagnostics logs (Settings blade).

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

Now you can see whats happening and much more detailed logging under Log Stream(Tools blade).

这篇关于如何使用 Azure Active Directory 向 azure 移动/Web 应用程序添加简单的身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆