如何简单验证添加到蔚蓝移动/ Web应用程序与Azure的活动目录? [英] How to add simple authentication to azure mobile/web apps with Azure Active Directory?

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

问题描述

我的目标是确保我的移动应用定制的API方法,并通过HttpClient的再使用(C#)。

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

为我所用邮差请求令牌,并用它来访问资源的测试的一部分。

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

推荐答案

我将创建一个新的移动应用时谈到香草模板解释。

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的活动目录>防爆preSS>创建新的AD应用

打开Active Directory(管理门户),挑目录您的帐户。

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

选择应用程序,您刚才创建的。

Select Applications, the one you just created.

转到配置选项卡和复制的客户端ID,创建一个密钥,复制它。

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

点击查看终点是在底部,并复制的OAuth 2.0令牌EndpointOauth2.0令牌

Click on View EndPoints at the bottom and copy the OAuth 2.0 Token EndpointOauth2.0 Token

现在打开移动应用项目和装饰要与[授权]项目控制器/方法。

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

您应该都设置与安装。

现在打开你喜欢的客户端,在我的情况和邮差

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).

现在你可以看到发生了什么,并根据更详细的日志记录的日志流(工具刀片)

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

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

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