使用PHP 获取 Azure Active Directory 令牌 的方法

一个新手
Lepaskan: 2023-03-15 20:20:01
asal
1211 orang telah melayarinya

在调用 Azure Rest API 时,如果是属于 Azure Resource Manager 的 API,则需要使用 Azure Active Directory (Azure AD)认证获取令牌(Token),然后才能够进行访问。

 以下是创建 Azure AD 应用,并授权其可以访问管理 Azure 的资源的步骤:

获取更好的阅读体验也可以点击此处。

 Note

以下认证方式,只适用于 Azure Resource Manager 的 API。 即 endpoint 为 management.chinacloudapi.cn 的 API,不适用于 Azure Service Manager 的 API(endpoint 为 management.core.chinacloudapi.cn 的 API)。

 登录 Azure 账户(PowerShell)

 记录获取到的 TenantID 以供后续程序使用。

选择当前订阅 ID

设置当前订阅,多订阅环境下需要执行该步骤 :

Set-AzureRmContext -SubscriptionId <subscription ID>
Salin selepas log masuk

创建 AD 应用

查看新创建的应用对象,属性 ApplicationId,在后续会用来创建服务凭证,角色设置和 Access Token。

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

创建服务凭证

Azure AD 应用创建服务凭证:

New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
Salin selepas log masuk

当创建完成服务凭证后,初始是没有任何权限的,我们需要为其设置权限范围。

授权

为您的服务凭证添加角色设置,在该例中,为您的服务凭证设置访问您订阅下所有资源的读权限。 如果想了解更多内容,请参考:Azure Role-based Access Control。

New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $azureAdApplication.ApplicationId
Salin selepas log masuk

其中 RoleDefinitionName 有三种权限设置:

  1. Reader 对Azure资源有读取权限。

  2. Contributor 对Azure资源有管理权限,但无法授权他人。

  3. Owner 对Azure资源拥有管理权限,也可以授权他人管理。

  

调用 Oauth2 API 获取 Token

这样 Azure AD Application 就创建完成了,我们可以使用以下三个信息,来获取认证的 Token。

  1. telent-id 对应订阅信息上使用的 telentID。

  2. application-id 创建应用返回的 ApplicationID。

  3. app password 创建应用时填写的密码。

获取 Token 的方式,使用 Azure login oauth2 的认证接口,如果想了解更多,请参考此文档:Using the Azure Resource Manager REST API。

请参考以下代码:

$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";
Salin selepas log masuk

执行查询后会得到 Token 数据, access_token 即为访问 Token。

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

然后将您要访问的 API 请求头上加上 Authorization 的 Header 设置,并将其值设置为:

Token 之前要加上 Bearer。

调用示例:

$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";
Salin selepas log masuk

Atas ialah kandungan terperinci 使用PHP 获取 Azure Active Directory 令牌 的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!