【问题标题】:How to authenticate to Azure REST API using username and password (no App Id)如何使用用户名和密码(无 App Id)对 Azure REST API 进行身份验证
【发布时间】:2018-03-02 09:38:12
【问题描述】:

我需要获取访问令牌以使用 REST API 访问 Azure(https://management.azure.com 端点)中的资源。每一篇读过的文章,都在用 Appliction Id 计数。就我而言,Azure 租户刚刚创建(以编程方式),我必须在其中创建一些资源。

我只有租户 ID、订阅 ID、管理员帐户的用户名和密码。如何仅使用我拥有的信息进行身份验证?它在 PowerShell 中是如何工作的,不需要使用 Application Id?

【问题讨论】:

  • AFAIK,你总是需要一个应用程序。 Azure PowerShell 也是一个应用程序。
  • 想知道您是否尝试使用不记名令牌,az account get-access-token -s 或使用用户名和密码获取不记名令牌以调用 API,例如 here。 Azure API 页面上的 try me 使用不记名令牌。

标签: rest powershell azure authentication


【解决方案1】:

据我所知,这是不可能的。正如 junnas 所说,即使您使用用户/密码身份验证,也需要客户端 ID。

在 Azure 上创建服务主体很容易,您可以查看link

sp创建完成后,会得到client id,client secret。您还需要在订阅时赋予 sp Owner 角色,您可以查看此link

现在,你可以使用 sp 来调用 Power Shell 中的 rest api,例如。

##get token
$TENANTID=""
$APPID=""
$PASSWORD=""
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token

##set subscriptionId and resource group name
$subscriptionId=""
$resourcegroupname="shui5"

$Headers=@{
    'authorization'="Bearer $token"
    'host'="management.azure.com"
    'contentype'='application/json'
}
$body='{
    "location": "northeurope",
     "tags": {
        "tagname1": "test-tag"
    }
 }'
Invoke-RestMethod  -Uri "https://management.azure.com/subscriptions/$subscriptionId/resourcegroups/${resourcegroupname}?api-version=2015-01-01"  -Headers $Headers -Method PUT -Body $body 

【讨论】:

  • 我知道如何注册应用程序并使用该应用程序进行身份验证。我需要自动化整个过程 - 创建租户(完成)并使用资源管理器添加资源,无需任何用户交互(尝试解决)。
  • @LukášNešpor 如果您的帐户是 Azure AD 帐户,这是可能的。您可以使用非交互式登录方式登录。你可以检查这个question
  • @LukášNešpor 然后您可以使用 Power Shell 创建服务主体。检查此official document。然后你可以得到tennat idclient idclient secretsubscription id。然后你可以使用它们来调用 API。
  • 另外,您需要在订阅级别赋予 sp Owner 角色,检查此link
  • 但是如果你的账号是微软账号,你需要先创建一个Azure AD账号,因为微软账号不支持非交互登录。此外,只有您可以使用它们来调用 Azure REST API 的帐户和密码,client idclient secret 是必需的。
【解决方案2】:

您可以使用 Microsoft PowerShell 应用程序 ID 进行身份验证,而无需拥有自己的应用程序 ID。此代码片段将为您提供令牌:

Import-Module MSOnline      # IMPORTANT! Loads type assembly Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext
$TENANTID = ""  # Your Tenant ID
$clientId = "1b730954-1685-4b74-9bfd-dac224a7b894"      # PowerShell Client Id
$MSMgmtURI = "https://management.core.windows.net"
$authority = "https://login.microsoftonline.com/$TENANTID"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($MSMgmtURI, $clientId, $redirectUri, "Always")  
$token = $authResult.AccessToken
$headers = @{'Authorization' = "Bearer $token", 'host'="management.azure.com", 'Content-Type' = "application/json"}

这对于使用登录对话框获取访问令牌非常有用。我怀疑 PowerShell 的客户端 ID 可以在上面的 Oath2 调用中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多