从 Azure Active Directory B2C 中删除用户 Android/Java
Deleting a user from Azure Active Directory B2C Android/Java
我有一个 Android 应用程序,我在其中使用 Azure AD B2C 对用户进行身份验证。用户根据需要登录和注销应用程序。我想让用户可以选择删除自己的帐户。
我知道我需要使用 Azure AD Graph API 来删除用户。这是我目前所拥有的:
根据 this link,似乎无法从个人帐户(B2C 用户使用的帐户)中删除用户。对吗?
这是我调用 Graph API 的代码片段。如果我偏离了轨道,请随意忽略它,并且有更好的方法来解决这个问题。
我认为我需要一个单独的访问令牌,而不是我的应用程序当前拥有的访问令牌(因为图表 API 需要其他 API 同意)。所以,我得到的访问令牌如下:
AcquireTokenParameters parameters = new AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(getActivity())
.fromAuthority(B2CConfiguration.getAuthorityFromPolicyName(B2CConfiguration.Policies.get("SignUpSignIn")))
.withScopes(B2CConfiguration.getGraphAPIScopes())
.withPrompt(Prompt.CONSENT)
.withCallback(getGraphAPIAuthCallback())
.build();
taxApp.acquireToken(parameters);
在 getGraphAPIAuthCallback()
方法中,我使用单独的线程(在后台)调用图表 API:
boolean resp = new DeleteUser().execute(authenticationResult.getAccessToken()).get();
最后,在我的 DeleterUser() AsyncTask
中,我正在执行以下操作:
@Override
protected Boolean doInBackground(String... aToken) {
final String asToken = aToken[0];
//this method will be running on background thread so don't update UI from here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider() {
@Override
public void authenticateRequest(final IHttpRequest request) {
request.addHeader("Authorization",
"Bearer " + asToken);
}
};
final IClientConfig mClientConfig = DefaultClientConfig
.createWithAuthenticationProvider(mAuthenticationProvider);
final IGraphServiceClient graphClient = new GraphServiceClient.Builder()
.fromConfig(mClientConfig)
.buildClient();
try {
graphClient.getMe().buildRequest().delete();
} catch (Exception e) {
Log.d(AccountSettingFragment.class.toString(), "Error deleting user. Error Details: " + e.getStackTrace());
}
return true;
}
目前,我的应用在尝试获取具有空指针异常的访问令牌时失败:
com.microsoft.identity.client.exception.MsalClientException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
知道我需要做什么才能为用户提供删除自己帐户的选项吗?谢谢!
有一行代码:graphClient.getUsers("").buildRequest().delete();
你好像没有把用户对象id放进去
但是,我们可以忽略这个问题,因为 Microsoft Graph 不允许用户删除自己。
这是我尝试执行时出现的错误。
{
"error": {
"code": "Request_BadRequest",
"message": "The principal performing this request cannot delete itself.",
"innerError": {
"request-id": "8f44118f-0e49-431f-a0a0-80bdd954a7f0",
"date": "2020-06-04T06:41:14"
}
}
}
感谢@allen-wu 的帮助。由于他的帮助,this azure feedback request and this azure doc,我能够弄清楚如何静默地获取和删除用户(无需干预)。
如@allen-wu 所述,您不能让用户删除自己。因此,我决定让移动应用程序在用户单击 'Delete Account' 按钮时调用我的 server-side NodeJS API(因为我不想将客户端密码存储在 android 应用程序)并让 NodeJS API 调用 Azure AD 端点以静默删除用户。一个警告是第一次尝试授权时需要管理员同意。另外,我只对 Graph API 进行了测试。我不是 100% 确定它是否也适用于其他 API。
步骤如下:
在您的 AAD B2C 租户中创建您的应用程序。创建一个客户端密钥并赋予它以下 API 权限: Directory.ReadWrite.All
;
AuditLog.Read.All
(我不是 100% 确定我们是否需要 AuditLog 权限。我还没有测试过)。
在浏览器中,粘贴以下内容link:
GET https://login.microsoftonline.com/{tenant}/adminconsent?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&state=12345
&redirect_uri=http://localhost/myapp/permissions
- 使用现有管理员帐户登录并同意该应用程序。
- 获得管理员同意后,您无需再次重复步骤 1-3。接下来,进行以下调用以获取访问令牌:
POST https://login.microsoftonline.com/{B2c_tenant_name}.onmicrosoft.com/oauth2/v2.0/token
在 body 中,包括您的 client_id
、client_secret
、grant_type
(其值应为 client_credentials
)和 scope
(值应为“https://graph.microsoft.com/.default”)
- 最后,您可以调用图表 API 来管理您的用户,包括删除他们:
DELETE https://graph.microsoft.com/v1.0/users/{upn}
不要忘记在 header 中包含访问令牌。我注意到在 Postman 中,图表 api 有一个错误,如果我在授权 header 的开头包含单词 'Bearer' 则返回错误。尝试没有它并且它有效。我还没有在我的 NodeJS API 中尝试过它,所以到目前为止还不能评论它。
@allen-wu 还建议使用 ROPC flow,我还没有尝试过,所以无法比较这两种方法。
希望对您有所帮助!
我有一个 Android 应用程序,我在其中使用 Azure AD B2C 对用户进行身份验证。用户根据需要登录和注销应用程序。我想让用户可以选择删除自己的帐户。
我知道我需要使用 Azure AD Graph API 来删除用户。这是我目前所拥有的:
根据 this link,似乎无法从个人帐户(B2C 用户使用的帐户)中删除用户。对吗?
这是我调用 Graph API 的代码片段。如果我偏离了轨道,请随意忽略它,并且有更好的方法来解决这个问题。
我认为我需要一个单独的访问令牌,而不是我的应用程序当前拥有的访问令牌(因为图表 API 需要其他 API 同意)。所以,我得到的访问令牌如下:
AcquireTokenParameters parameters = new AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(getActivity())
.fromAuthority(B2CConfiguration.getAuthorityFromPolicyName(B2CConfiguration.Policies.get("SignUpSignIn")))
.withScopes(B2CConfiguration.getGraphAPIScopes())
.withPrompt(Prompt.CONSENT)
.withCallback(getGraphAPIAuthCallback())
.build();
taxApp.acquireToken(parameters);
在 getGraphAPIAuthCallback()
方法中,我使用单独的线程(在后台)调用图表 API:
boolean resp = new DeleteUser().execute(authenticationResult.getAccessToken()).get();
最后,在我的 DeleterUser() AsyncTask
中,我正在执行以下操作:
@Override
protected Boolean doInBackground(String... aToken) {
final String asToken = aToken[0];
//this method will be running on background thread so don't update UI from here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider() {
@Override
public void authenticateRequest(final IHttpRequest request) {
request.addHeader("Authorization",
"Bearer " + asToken);
}
};
final IClientConfig mClientConfig = DefaultClientConfig
.createWithAuthenticationProvider(mAuthenticationProvider);
final IGraphServiceClient graphClient = new GraphServiceClient.Builder()
.fromConfig(mClientConfig)
.buildClient();
try {
graphClient.getMe().buildRequest().delete();
} catch (Exception e) {
Log.d(AccountSettingFragment.class.toString(), "Error deleting user. Error Details: " + e.getStackTrace());
}
return true;
}
目前,我的应用在尝试获取具有空指针异常的访问令牌时失败:
com.microsoft.identity.client.exception.MsalClientException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
知道我需要做什么才能为用户提供删除自己帐户的选项吗?谢谢!
有一行代码:graphClient.getUsers("").buildRequest().delete();
你好像没有把用户对象id放进去
但是,我们可以忽略这个问题,因为 Microsoft Graph 不允许用户删除自己。
这是我尝试执行时出现的错误。
{
"error": {
"code": "Request_BadRequest",
"message": "The principal performing this request cannot delete itself.",
"innerError": {
"request-id": "8f44118f-0e49-431f-a0a0-80bdd954a7f0",
"date": "2020-06-04T06:41:14"
}
}
}
感谢@allen-wu 的帮助。由于他的帮助,this azure feedback request and this azure doc,我能够弄清楚如何静默地获取和删除用户(无需干预)。
如@allen-wu 所述,您不能让用户删除自己。因此,我决定让移动应用程序在用户单击 'Delete Account' 按钮时调用我的 server-side NodeJS API(因为我不想将客户端密码存储在 android 应用程序)并让 NodeJS API 调用 Azure AD 端点以静默删除用户。一个警告是第一次尝试授权时需要管理员同意。另外,我只对 Graph API 进行了测试。我不是 100% 确定它是否也适用于其他 API。
步骤如下:
在您的 AAD B2C 租户中创建您的应用程序。创建一个客户端密钥并赋予它以下 API 权限:
Directory.ReadWrite.All
;AuditLog.Read.All
(我不是 100% 确定我们是否需要 AuditLog 权限。我还没有测试过)。在浏览器中,粘贴以下内容link:
GET https://login.microsoftonline.com/{tenant}/adminconsent?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&state=12345
&redirect_uri=http://localhost/myapp/permissions
- 使用现有管理员帐户登录并同意该应用程序。
- 获得管理员同意后,您无需再次重复步骤 1-3。接下来,进行以下调用以获取访问令牌:
POST https://login.microsoftonline.com/{B2c_tenant_name}.onmicrosoft.com/oauth2/v2.0/token
在 body 中,包括您的 client_id
、client_secret
、grant_type
(其值应为 client_credentials
)和 scope
(值应为“https://graph.microsoft.com/.default”)
- 最后,您可以调用图表 API 来管理您的用户,包括删除他们:
DELETE https://graph.microsoft.com/v1.0/users/{upn}
不要忘记在 header 中包含访问令牌。我注意到在 Postman 中,图表 api 有一个错误,如果我在授权 header 的开头包含单词 'Bearer' 则返回错误。尝试没有它并且它有效。我还没有在我的 NodeJS API 中尝试过它,所以到目前为止还不能评论它。
@allen-wu 还建议使用 ROPC flow,我还没有尝试过,所以无法比较这两种方法。
希望对您有所帮助!