要在Android上实现Active Directory B2C授权,可以按照以下步骤进行操作:
build.gradle
文件中添加以下依赖项:dependencies {
implementation 'com.microsoft.identity.client:msal:1.4.0'
}
AuthenticationHelper
类,用于处理授权逻辑。代码示例:import android.app.Activity;
import android.util.Log;
import com.microsoft.identity.client.AuthenticationCallback;
import com.microsoft.identity.client.AuthenticationResult;
import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.IPublicClientApplication;
import com.microsoft.identity.client.PublicClientApplication;
public class AuthenticationHelper {
private static final String TAG = "AuthenticationHelper";
private IPublicClientApplication mPublicClientApplication;
public AuthenticationHelper(Activity activity, String clientId, String authority) {
try {
mPublicClientApplication = PublicClientApplication.createSingleAccountPublicClientApplication(activity, clientId, authority);
} catch (Exception e) {
Log.e(TAG, "Failed to create PublicClientApplication: " + e.getMessage());
}
}
public void signIn(Activity activity, AuthenticationCallback authenticationCallback) {
mPublicClientApplication.signIn(activity, null, null, authenticationCallback);
}
public void signOut() {
mPublicClientApplication.signOut();
}
public IAccount getAccount() {
return mPublicClientApplication.getAccount();
}
}
AuthenticationHelper
类来实现授权。代码示例:import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.microsoft.identity.client.AuthenticationCallback;
import com.microsoft.identity.client.AuthenticationResult;
import com.microsoft.identity.client.IAccount;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_SIGN_IN = 123;
private AuthenticationHelper mAuthenticationHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuthenticationHelper = new AuthenticationHelper(this, "your_client_id", "your_authority");
findViewById(R.id.btnSignIn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
findViewById(R.id.btnSignOut).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
}
private void signIn() {
mAuthenticationHelper.signIn(this, new AuthenticationCallback() {
@Override
public void onSuccess(AuthenticationResult authenticationResult) {
Log.d(TAG, "Sign in success");
}
@Override
public void onError(Exception exception) {
Log.e(TAG, "Sign in error: " + exception.getMessage());
}
@Override
public void onCancel() {
Log.d(TAG, "Sign in canceled");
}
});
}
private void signOut() {
mAuthenticationHelper.signOut();
Log.d(TAG, "Sign out success");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mAuthenticationHelper.getAccount().onActivityResult(requestCode, resultCode, data);
}
}
请注意替换代码中的your_client_id
和your_authority
为你自己的客户端ID和Authority。
这样,你就可以在Android上使用Active Directory B2C进行授权了。