search
HomeBackend DevelopmentPHP TutorialDetailed example of how php obtains Azure Active Directory token

When calling the Azure Rest API, if it is an API belonging to Azure Resource Manager, you need to use Azure Active Directory (Azure AD) authentication to obtain a token (Token) before you can access it.

The following are the steps to create an Azure AD application and authorize it to access resources that manage Azure:

For a better reading experience, you can also click here.

Note

The following authentication methods are only applicable to Azure Resource Manager API. That is, the API with the endpoint management.chinacloudapi.cn is not applicable to the API of Azure Service Manager (the API with the endpoint management.core.chinacloudapi.cn).

Log in to your Azure account (PowerShell)

##Record the obtained TenantID for subsequent use.

Select the current subscription ID

Set the current subscription. This step needs to be performed in a multi-subscription environment:


Set-AzureRmContext -SubscriptionId <subscription ID>

Create AD application

View the newly created application object and the attribute ApplicationId, which will be used to create service credentials, role settings and Access Token later.


$azureAdApplication = New-AzureRmADApplication -DisplayName "exampleapp" -HomePage "https://www.contoso.org" -IdentifierUris "https://www.contoso.org/example" -Password "<Your_Password>"

Create Service Credentials

Azure AD Application Create Service Credentials:


New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId

After the service credential is created, it does not have any permissions initially. We need to set the permission scope for it.

Authorization

Add role settings for your service credentials. In this example, set read permissions for your service credentials to access all resources under your subscription. If you want to learn more, please refer to: Azure Role-based Access Control.


New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $azureAdApplication.ApplicationId

There are three permission settings for

RoleDefinitionName:

  1. Reader has read permissions for Azure resources.

  2. Contributor has administrative rights to Azure resources, but cannot authorize others.

  3. Owner has management rights to Azure resources and can also authorize others to manage them.

 

Call Oauth2 API to obtain Token

In this way, the Azure AD Application is created and we can use it The following three pieces of information are used to obtain the authentication Token.

  1. telent-id corresponds to the telentID used in subscription information.

  2. application-id ApplicationID returned by creating the application.

  3. app password The password filled in when creating the application.

To obtain the Token, use the authentication interface of Azure login oauth2. If you want to know more, please refer to this document: Using the Azure Resource Manager REST API.

Please refer to the following code:


$tenlent_id = &#39;Your Sub Tenlent ID&#39;;
$client_id = &#39;Application ID&#39;;
$client_secret = &#39;Application Password&#39;;

$auth_url = &#39;https://login.chinacloudapi.cn/&#39;.$tenlent_id.&#39;/oauth2/token?api-version=1.0&#39;;
$auth = curl_init($auth_url);
$post_data= &#39;grant_type=client_credentials&resource=https://management.chinacloudapi.cn/&client_id=&#39;.$client_id.&#39;&client_secret=&#39;.urlencode($client_secret);

curl_setopt_array($auth, array(
CURLOPT_VERBOSE => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
&#39;Content-Type: application/x-www-form-urlencoded&#39;
)
));
curl_exec($atuh);
echo "\n";

After executing the query, you will get the Token data, and access_token is the access Token.


{
"token_type": "Bearer",
"expires_in": "3600",
"expires_on": "1455680701",
"not_before": "1455676801",
"resource": "https://management.azure.com/",
"access_token": "eyJ0eXAiOi…"
}

Then add the Authorization Header setting to the API request you want to access, and set its value to:

Bearer must be added before Token.

Call example:


$token = &#39;eyJ0eXA…&#39;;
$host = &#39;management.chinacloudapi.cn&#39;;
$version = &#39;2015-09-01&#39;;
$url = &#39;https://&#39;.$host.&#39;/subscriptions/5bbf0cbb-647d-4bd8-b4e6-26629f109bd7/resourceGroups/Default-MySql-ChinaNorth/providers/Microsoft.MySql/servers/poddbtest/databases/kevintest?api-version=&#39;.$version;
$ch = curl_init($url);
$data = array(
&#39;properties&#39; => array(
&#39;charset&#39; => &#39;utf8&#39;,
&#39;collation&#39; => &#39;utf8_general_ci&#39;
),
);
$json = json_encode($data);

curl_setopt_array($ch, array(
CURLOPT_VERBOSE => 1,
CURLOPT_CUSTOMREQUEST => &#39;PUT&#39;,
CURLOPT_POSTFIELDS => $json,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
&#39;Content-type:application/json&#39;,
&#39;Authorization:Bearer &#39;.$token
)
));

$ret =curl_exec($ch);
if (empty($ret)) {
    // some kind of an error happened
    echo &#39;Curl error: &#39; . curl_error($ch);
} else {
    $info = curl_getinfo($ch);
}
echo "\n";

The above is the detailed content of Detailed example of how php obtains Azure Active Directory token. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
错误代码 801c03ed:如何在 Windows 11 上修复它错误代码 801c03ed:如何在 Windows 11 上修复它Oct 04, 2023 pm 06:05 PM

错误801c03ed通常附带以下消息:管理员策略不允许此用户加入设备。此错误消息将阻止你安装Windows并加入网络,从而阻止你使用电脑,因此尽快解决此问题非常重要。什么是错误代码801c03ed?这是一个Windows安装错误,由于以下原因而发生:Azure设置不允许新用户加入。Azure上未启用设备对象。Azure面板中的硬件哈希出现故障。如何修复Windows801上的错误代码03c11ed?1.检查Intune设置登录到Azure门户。导航到“设备”,然后选择“设备设置”。将“用户可以将

Active Directory 用户和计算机丢失 [以 3 种方式修复]Active Directory 用户和计算机丢失 [以 3 种方式修复]Apr 20, 2023 pm 01:25 PM

ActiveDirectory用户和计算机(ADUC)丢失是许多WindowsPro用户报告的最令人沮丧的问题之一。ADUC是一个令人难以置信的MMC管理单元,它使管理员能够管理MicrosoftActiveDirectory。但是,由于某种原因,它在Windows服务器或Enterprise/Pro版本中缺失。让我们深入了解它丢失的原因以及我们如何修复它。Windows11是否有ActiveDirectory?ActiveDirectory是任何想要管理远程

利用Azure语义搜索与OpenAI,打造认知搜索系统利用Azure语义搜索与OpenAI,打造认知搜索系统Oct 12, 2023 am 10:18 AM

旨在简化文档搜索,多种服务和平台的结合都是获得无与伦比性能的关键。在本文中,我们将探索一种将 Azure 认知服务的强大功能与 OpenAI 的功能相结合的整体方法。通过深入研究意图识别、文档过滤、特定领域的算法和文本摘要,您将学习创建一个不仅可以理解用户意图而且可以有效处理和呈现信息的系统。

微软 Azure OpenAI 服务现已支持 GPT-4 Turbo with Vision微软 Azure OpenAI 服务现已支持 GPT-4 Turbo with VisionDec 18, 2023 am 08:18 AM

本站12月17日消息,AzureOpenAI服务提供对OpenAI强大语言模型的RESTAPI访问,这些模型包括GPT-4、GPT-3.5-Turbo和嵌入模型系列。微软宣布进一步加强AzureOpenAI服务的功能,并为客户提供了最新的GPT-4TurbowithVision的公开预览版本这个先进的多模态AI模型继承了GPT-4Turbo的所有强大功能,并且还加入了图像处理和分析的能力。这样一来,我们就有了利用GPT-4处理更多任务的机会,比如改进无障碍辅助功能、解释和分析可视化数据,以及进行

微软 Azure 将从 10 月开始推行强制性多因素身份验证功能微软 Azure 将从 10 月开始推行强制性多因素身份验证功能Aug 17, 2024 am 07:40 AM

本站8月16日消息,微软昨日发布了一条公告,宣布将从10月开始强制实施多因素身份验证(MFA)功能,以帮助降低账户被入侵的可能性。微软表示,MFA可以阻止99.2%以上的此类账户入侵攻击,因此该措施是强制性的。据介绍,这一过程将分为两个阶段逐步实施,本站总结如下:第1阶段:从10月开始,登录Azure门户、MicrosoftEntra管理中心和Intune管理中心都将需要MFA,但不会影响其他Azure客户端。第2阶段:从2025年初开始,MFA要求将逐步拓展到其他Azure客户端,例如Azur

Microsoft Security 通过多云功能升级用户防护能力Microsoft Security 通过多云功能升级用户防护能力Apr 24, 2023 pm 09:07 PM

微软详细介绍了它计划如何通过将多云支持引入其主要安全策略来进一步保护客户的安全。与此相一致,该公司旨在引入新功能和进步,以帮助从集中管理的角度加强安全性和控制。为了尝试向其客户引入更简单的安全解决方案,以便他们不会发现管理多云环境具有挑战性,微软决定将“MicrosoftDefenderforCloud的本机功能扩展到谷歌云平台(GCP)”。得益于GCP的支持,微软成为唯一一家为业界前三大平台提供原生多云保护的云提供商,这些平台分别是:微软Azure、亚马逊网络服务(AWS)

Windows Server VNext Preview 内部版本 25099 现已推出Windows Server VNext Preview 内部版本 25099 现已推出Apr 14, 2023 pm 01:55 PM

微软已发布 Windows Server Insider Preview 的新版本。本周的 build 25099 现在可以从Windows Server Insider 网站下载 ISO和 VHDX,但按照惯例,没有完整的变更日志可言,因此不清楚有什么新内容。微软再次表示该品牌尚未更新,在预览版中仍为 Windows Server 2022。此外,微软鼓励内部人员在Azure

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.