【发布时间】:2020-06-04 06:21:37
【问题描述】:
我有一个 Android 应用程序,我在其中使用 Azure AD B2C 对用户进行身份验证。用户根据需要登录和注销应用程序。我想为用户提供删除自己帐户的选项。
我了解我需要使用 Azure AD Graph API 来删除用户。这是我目前所拥有的:
根据this link,似乎无法从个人帐户(这是 B2C 用户正在使用的帐户)中删除用户。对吗?
这是我用于 Graph API 调用的代码 sn-p。如果我偏离了轨道,请随意忽略它,并且有更好的方法来解决这个问题。
我认为我需要一个单独的访问令牌,而不是我的应用当前拥有的访问令牌(因为图形 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() 方法中,我使用单独的线程(在后台)调用 Graph 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
知道我需要做什么来为用户提供删除自己帐户的选项吗?谢谢!
【问题讨论】:
标签: android azure-active-directory azure-ad-b2c azure-ad-graph-api