【问题标题】:Azure ADAL for Python - facing issue with acquire_token_with_username_password methodAzure ADAL for Python - 面临acquire_token_with_username_password 方法的问题
【发布时间】:2017-06-08 15:55:43
【问题描述】:

我正在尝试使用 azure user id 和 password 检索访问令牌。 最初我尝试使用以下 python 代码块

import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
    "https://management.azure.com/",
    CLIENT_ID,
    CLIENT_SECRET)

这是返回令牌没有任何问题。 我正在按照https://github.com/AzureAD/azure-activedirectory-library-for-python 的示例使用用户名和密码检索令牌,代码块如下

token2 = context.acquire_token_with_username_password("https://management.azure.com/",USER_NAME,PASSWORD,CLIENT_ID)

在这种情况下,不返回任何令牌,而是返回以下响应。

File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 145, in acquire_token_with_username_password
    return self._acquire_token(token_func)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 109, in _acquire_token
    return token_func(self)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 143, in token_func
    return token_request.get_token_with_username_password(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 286, in get_token_with_username_password
    token = self._get_token_username_password_federated(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 252, in _get_token_username_password_federated
    username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 211, in _perform_username_password_for_access_token_exchange
    username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 198, in _perform_wstrust_exchange
    result = wstrust.acquire_token(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\wstrust_request.py", line 160, in acquire_token
    raise AdalError(return_error_string, error_response)
adal.adal_error.AdalError: WS-Trust RST request returned http error: 500 and server response: <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2017-06-07T12:12:56.567Z</u:Created><u:Expires>2017-06-07T12:17:56.567Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value><s:Subcode><s:Value xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:FailedAuthentication</s:Value></s:Subcode></s:Code><s:Reason><s:Text xml:lang="en-US">ID3242: The security token could not be authenticated or authorized.</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>

Process finished with exit code 1

如果有人对此有任何想法,请告诉我。

【问题讨论】:

  • 在使用用户名/密码的公共 Azure 上,您的 AUTHORITY 可能是“login.microsoftonline.com/common”。否则你能分享你的权威吗? RESOURCE 也应该是“management.core.windows.net
  • @LaurentMazuel 资源https://management.core.windows.net 用于Azure 服务管理,资源https://management.azure.com 用于Azure 资源管理。
  • 为 management.core.windows.net 颁发的令牌也适用于 management.azure.com。试试看。

标签: python azure adal azure-sdk-python


【解决方案1】:

根据错误堆栈的信息,根据我的经验,由于错误来自方法_get_token_username_password_federated和WS-Trust RST请求的500错误代码,您用于获取令牌的用户/密码似乎不是在您的应用注册的 Azure AD 中创建,但看起来像在联合 AD 中创建的。

请尝试通过以下两种方式检查问题。

  1. 使用管理员帐户在 Azure 门户上注册的应用的 Azure AD 中创建一个新用户,然后使用新用户/密码检索令牌。
  2. 检查您的 Azure AD 配置以确保是否已部署 Active Directory 联合身份验证服务以及在联合 AD 实例中创建的当前使用/密码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多