azure - 使用托管标识对 Azure Active Directory 应用程序注册进行 HTTP 请求失败,并在 Azure 中出现未经授权的错误

标签 azure powershell azure-active-directory azure-managed-identity

在使用“企业应用程序”托管标识向 Azure 中的 Azure Active Directory 应用程序注册发出 http 请求时,即使在分配托管标识“application.readwrite.all”权限后,我也会收到“未经授权”错误。

向应用程序注册发出 GET 请求:https://learn.microsoft.com/en-us/graph/api/application-get?view=graph-rest-1.0&tabs=http

我正在使用 PowerShell 发出应用注册请求:

$token=<Access token from managed identity>
$headers = @{
    Authorization="Bearer $token"
}

$serviceUri="https://graph.microsoft.com/v1.0/applications/$applicationId"

$httpResponse = Invoke-WebRequest -Uri $serviceUri -Method Get -Headers $headers -ContentType application/json

有人可以帮助我了解需要为托管身份分配哪些权限以及如何成功向 Azure Active Directory 发送 https 请求吗?

在将“application.readwrite.all”权限分配给托管身份后,我尝试了 PATCH 请求、GET 请求。

我尝试了带有和不带有内容类型的“Invoke-WebRequest”。

我尝试使用 uri:“https://graph.microsoft.com/v1.0/me”查询用户上下文,这也因未经授权的错误而失败。

最佳答案

我尝试在我的环境中重现相同的内容,但得到了如下所示的相同错误:

enter image description here

如果访问 token 没有足够的权限来执行操作,通常会发生此错误。

要解决该错误,请尝试以下操作:

我创建了 Azure 应用服务并启用了系统托管标识:

enter image description here

现在,我为企业应用程序分配了 Application.ReadWrite.All 权限,如下所示:

enter image description here

我使用以下脚本生成了托管身份的访问 token :

$resourceURI = "https://graph.microsoft.com/" 
$tokenAuthURI = $env:IDENTITY_ENDPOINT + "?resource=$resourceURI&api-version=2019-08-01"  
$tokenResponse = Invoke-RestMethod -Method Get -Headers @{"X-IDENTITY-HEADER"="$env:IDENTITY_HEADER"} -Uri $tokenAuthURI  
$accessToken = $tokenResponse.access_token

enter image description here

在解码 jwt.ms 中的 token 时,请确保存在 Application.ReadWrite.All 权限。 :

enter image description here

我能够使用访问 token 成功获取应用程序详细信息:

$token=<Access token from managed identity>
$headers = @{
    Authorization="Bearer $token"
}

$serviceUri="https://graph.microsoft.com/v1.0/applications/$applicationId"

$httpResponse = Invoke-WebRequest -Uri $serviceUri -Method Get -Headers $headers -ContentType application/json

回应:

enter image description here

StatusCode : 200  
StatusDescription : OK  
Content : {"@odata.context":"[https://graph.microsoft.com/v1.0/$metadata#applications","value":[{"id":"d-d2a5-47b2-b7b2-a3af06b9d8ea","deletedDateTime":null,"appId":"2c8cd94f-1fbd-43f]
3-bf74-2699b3838007",...  
RawContent : HTTP/1.1 200 OK  
Transfer-Encoding: chunked  
Strict-Transport-Security: max-age=31536000  
request-id: 4540ad41-c127-4feea271  
client-request-id: 4540ad41-c127-4fee-90eb-408cd5d71  
x-m...  
Forms : {}  
Headers : {[Transfer-Encoding, chunked], [Strict-Transport-Security, max-age=31536000], [request-id, 4540ad41-c127-4fee-90eb-408cd5d5a271], [client-request-id,  
4540ad41-c127-4fee-90eb-408cd5d5a271]...}  
Images : {}  
InputFields : {}  
Links : {}  
ParsedHtml : mshtml.HTMLDocumentClass  
RawContentLength : 8142

关于azure - 使用托管标识对 Azure Active Directory 应用程序注册进行 HTTP 请求失败,并在 Azure 中出现未经授权的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76001482/

相关文章:

security - Azure Active Directory v2.0 电子邮件地址验证

azure - 有一些如何备份 Azure AD B2C 的方法吗?

azure - Terraform Cloud 无法在防火墙后面的存储帐户中创建容器

powershell - Send-MailMessage 使附件变大

c# - System.FormatException : Element 'HoleId' does not match any field or property of class build. 模型.产品

c# - 无法在 C# 中运行禁用邮箱 Powershell

image - 我可以使用powershell打印图像吗?

Azure 应用程序错误地请求管理员同意?

asp.net - 在 azure 上的 asp.net vnext 网站上禁用端口 80

Azure Log4Net Appender 避免为每个条目创建 xml 文件