Ash*_*ain 5 azure azure-active-directory azure-devops
我正在尝试使用以下代码获取服务主体的访问令牌。
\n$authUrl = "https://login.windows.net/" + $tenantid + "/oauth2/token/"\n$body = @{\n grant_type = "client_credentials"\n client_id = $serviceprincipalid\n resource = "https://management.azure.com/"\n client_secret = $serviceprincipalkey\n};\n$response = Invoke-RestMethod \xe2\x80\x93Uri $authUrl \xe2\x80\x93Method POST \xe2\x80\x93Body $body\n\nWrite-Host $response\nWrite-Output $response.access_token\n##vso[task.setvariable variable=myToken;]$response.access_token\n
Run Code Online (Sandbox Code Playgroud)\n上面的代码在我的本地计算机的 PowerShell 上运行良好,但当我在 Azure DevOps 管道中运行相同的代码库时,它给出以下错误。
\nClientSecretCredential authentication failed: A configuration issue is preventing \nauthentication - check the error message from the server for details. You can modify the \nconfiguration in the application registration portal. See https://aka.ms/msal-net-invalid- \nclient for details. Original exception: AADSTS7000222: The provided client secret keys are \nexpired. Visit the Azure Portal to create new keys for your app, or consider using certificate \ncredentials for added security: https://learn.microsoft.com/azure/active- \ndirectory/develop/active-directory-certificate-credentials\nTrace ID: 98787ui7-e8ae-4712-b8b5-7678u8765rt5\nCorrelation ID: yhjnbv43-56sy-9ksy-b8b5-mj876yu78i90\nTimestamp: 2021-03-16 12:32:28Z\nThere was an error with the service principal used for the deployment.`\n
Run Code Online (Sandbox Code Playgroud)\n\n我检查了密钥,但密钥没有过期,它的过期日期已经设置为 2022 年。如果它过期,那么代码不应该在我本地计算机的 PowerShell 上运行。
有人有什么主意吗?请让我知道解决这个问题。
\n嗯,实际上这个错误不是由上面的脚本引起的,根据我的测试,它在 devops 中工作得很好。
如果您使用Azure PowerShell task
,它将让您配置要使用的服务连接,当您运行任务时,它将自动将 Azure powershell 与服务连接中配置的服务主体连接起来。该错误是由服务连接中配置的服务主体的机密过期引起的,而不是脚本中的机密。
我也可以在我这边重现您的问题。
要解决此问题,请按照以下步骤操作。
1.导航到 Azure PowerShell 任务,检查您使用的服务连接。
2.导航到Project Settings
devops -> Service connections
-> 找到您使用的 -> 单击它 -> Manage Service Principal
。
然后它将打开相关的 AD App 页面,只需创建一个新的密钥和服务连接,在 Azure Powershell 任务中使用它,按照我在这里提到的相同步骤操作。
3.配置完成后,再次测试,可以正常工作。
归档时间: |
|
查看次数: |
33997 次 |
最近记录: |