繁体   English   中英

如何使用图形 api 更新 azure 广告中的用户密码

[英]How to update the password of user in azure ad using graph api

我正在使用 Microsoft graph API 来更新用户的密码。 我有以下 json 请求:

{
    "accountEnabled": true,
    "userPrincipalName": "testuser34@mytenantname.onmicrosoft.com",
    "passwordProfile": {
        "forceChangePasswordNextSignIn": false,
        "password": "<new_password>"
    }
}

我使用PATCH方法调用此url https://graph.microsoft.com/v1.0/users/testuser34@mytenantname.onmicrosoft.com 我还将不记名令牌作为授权 header 传递,但出现以下错误:

{
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
      "request-id": "12781f2a-0cdd-449d-94d7-aae1440a7559",
      "date": "2020-04-03T02:52:57"
    }
  }
}

错误说Insufficient privileges 根据我关注的这个页面,它说:

When updating the passwordProfile property, the following permission is required: Directory.AccessAsUser.All.

我已经在创建的应用程序中检查了这个,并且这个权限在应用程序注册中

在此处输入图像描述

但我仍然收到此错误。 任何人都可以帮助解决此错误。 谢谢

编辑:在 Microsoft 中也添加了权限,但仍然出现相同的错误

在此处输入图像描述

编辑2:

我有以下代码,我从中获取令牌:

data = {    
            "grant_type": "client_credentials",
            "client_secret": <client_secret>,
            "client_id": <client_id>,
            "resource": "https://graph.microsoft.com"
       }

r = requests.post("https://login.microsoftonline.com/<tennant_id>/oauth2/token", data)

if r.status_code == 200:
    ret_body = r.json()
    token = ret_body['access_token']
else:
    log.error("Unable to get token from oauth {}, {}".format(r.status_code, r.json()))

编辑3:

我还在用户管理员中添加了应用名称:

在此处输入图像描述

但问题仍然存在。

您正在使用Microsoft Graph ,因此您需要在Microsoft Graph中添加权限,而不是Azure Active Directory Graph 在此处输入图像描述

更新:

您使用的是客户端凭据流程,使用此流程时,需要Application permission ,如果您想获取令牌以更新passwordProfile ,则需要Delegated 权限Directory.AccessAsUser.All来更新具有Application permission的用户,我们最多只能添加Directory.ReadWrite.All ,但是这个权限不能reset user passwords

从最低权限到最高权限检查权限

在此处输入图像描述

然后查看Directory.ReadWrite.All备注

Directory.ReadWrite.All

解决方案:

要使用客户端凭据流更新passwordProfile ,请将 AD 应用程序的服务主体添加为 Azure AD 中的目录角色。

在门户中导航到Azure Active Directory -> Roles and administrators -> User administrator -> Add assignments -> 搜索您的 AD 应用程序的名称 -> Add

然后获取token调用API,就可以正常工作了:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM