使用图形更改 Azure AD B2C 用户密码 API

Change Azure AD B2C User Password with Graph API

我正在尝试使用 Sample Graph API 应用更改用户密码,但我得到:

调用图形时出错API响应:

{
  "odata.error": {
    "code": "Authorization_RequestDenied",
    "message": {
      "lang": "en",
      "value": "Insufficient privileges to complete the operation."
    }
  }
}

图表API请求:

PATCH /mytenant.onmicrosoft.com/users/some-guid?api-version=1.6 HTTP/1.1
client-request-id: ffd564d3-d716-480f-a66c-07b02b0e32ab
date-time-utc: 2017.08.10 03:04 PM

JSON 文件

{
    "passwordProfile": {
        "password": "Somepassword1$",
        "forceChangePasswordNextLogin": false
    }
}

我已经测试过更新用户的 displayName 并且效果很好。

{
    "displayName": "Joe Consumer"
}

AD 应用程序权限

我已配置 my app permissions as described here.

查看 this article。好像是一样的症状。

解决方案一:

如果您在调用仅包含读取权限的 API 时收到此错误,则必须在 Azure 管理门户中设置权限。

  • 转到 Azure 管理门户并单击 Active Directory。
  • Select 您的自定义 AD 目录。
  • 单击“应用程序”并 select 您的应用程序。
  • 单击配置并向下滚动到部分 'Permissions to other applications'。
  • 为 Windows Azure Active Directory 提供所需的应用程序权限和委派权限。
  • 最后保存更改。

方案二:

如果您在调用包含 deletereset password 操作的 API 时收到此错误,那是因为这些操作需要 Company Administrator 的管理员角色.截至目前,您只能通过 Azure AD Powershell module 添加此角色。

  1. 使用 Get-MsolServicePrincipal –AppPrincipalId 查找服务主体

    Get-MsolServicePrincipal | ft DisplayName, AppPrincipalId -AutoSize
    
  2. 使用 Add-MsolRoleMember 将其添加到 Company Administrator 角色

    $clientIdApp = 'your-app-id'
    $webApp = Get-MsolServicePrincipal –AppPrincipalId $clientIdApp
    
    Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberType ServicePrincipal -RoleMemberObjectId $webApp.ObjectId
    

要通过 PowerShell 连接到您的 B2C 租户,您需要一个本地管理员帐户。 This blog post should help 见 "The Solution" 部分。

尝试以下设置,适合我。

使用了下面的JSON

 {
  "accountEnabled": true,
  "signInNames": [
    {
      "type": "emailAddress",
      "value": "kart.kala1@test.com"
    }
  ],
  "creationType": "LocalAccount",
  "displayName": "Joe Consumer",
  "mailNickname": "joec",
  "passwordProfile": {
    "password": "P@$$word!",
    "forceChangePasswordNextLogin": false
  },
  "passwordPolicies": "DisablePasswordExpiration",
  "givenName": "Joe",
}

还要确保为应用程序分配用户帐户和管理员角色,这将允许它删除用户 link here