單一和多個帳戶公用用戶端應用程式

本文將協助您瞭解單一帳戶和多個帳戶公用用戶端應用程式中使用的類型,並著重於單一帳戶公用用戶端應用程式。

Microsoft 驗證連結庫 (MSAL) 會為您的用戶端應用程式建立模型。 大部分的Android應用程式都會被視為公用用戶端。 公用客戶端是無法安全地保留秘密的應用程式。

MSAL 專門提供的 API 介面 PublicClientApplication ,以簡化和釐清應用程式的開發體驗,而應用程式一次只能使用一個帳戶。 PublicClientApplication 是由 SingleAccountPublicClientApplicationMultipleAccountPublicClientApplication子類別化。 下圖顯示這些類別之間的關聯性。

SingleAccountPublicClientApplication UML Class Diagram

單一帳戶公用用戶端應用程式

類別 SingleAccountPublicClientApplication 可讓您建立 MSAL 型應用程式,只允許一次登入單一帳戶。 SingleAccountPublicClientApplicationPublicClientApplication 在下列各方面不同:

  • MSAL 會追蹤目前登入的帳戶。
    • 如果您的應用程式使用訊息代理程式(Azure 入口網站 應用程式註冊期間的預設值),且安裝在代理程式存在的裝置上,MSAL 會確認該帳戶仍可在裝置上使用。
  • signIn 可讓您明確地和個別地登入帳戶,以及要求範圍。
  • acquireTokenSilent 不需要帳戶參數。 如果您確定提供帳戶,而且您提供的帳戶不符合 MSAL 所追蹤的目前帳戶, MsalClientException 則會擲回 。
  • acquireToken 不允許使用者切換帳戶。 如果用戶嘗試切換至不同的帳戶,則會擲回例外狀況。
  • getCurrentAccount 會傳回提供下列項目的結果物件:
    • 布爾值,指出帳戶是否已變更。 例如,帳戶可能會因為從裝置中移除而變更。
    • 先前的帳戶。 如果您需要在從裝置移除帳戶或登入新帳戶時,執行任何本機數據清除,這會很有用。
    • currentAccount。
  • signOut 從裝置移除與客戶端相關聯的任何令牌。

當裝置上安裝 Microsoft Authenticator 或 Intune 公司入口網站 之類的 Android 驗證代理程式,且您的應用程式設定為使用訊息代理程式時,signOut將不會從裝置移除帳戶。

單一帳戶案例

下列虛擬程式代碼說明如何使用 SingleAccountPublicClientApplication

// Construct Single Account Public Client Application
ISingleAccountPublicClientApplication app = PublicClientApplication.createSingleAccountPublicClientApplication(getApplicationContext(), R.raw.msal_config);

String[] scopes = {"User.Read"};
IAccount mAccount = null;

// Acquire a token interactively
// The user will get a UI prompt before getting the token.
app.signIn(getActivity(), scopes, new AuthenticationCallback()
{

        @Override
        public void onSuccess(IAuthenticationResult authenticationResult) 
        {
            mAccount = authenticationResult.getAccount();
        }

        @Override
        public void onError(MsalException exception)
        {
        }

        @Override
        public void onCancel()
        {
        }
    }
);

// Load Account Specific Data
getDataForAccount(account);

// Get Current Account
ICurrentAccountResult currentAccountResult = app.getCurrentAccount();
if (currentAccountResult.didAccountChange())
{
    // Account Changed Clear existing account data
    clearDataForAccount(currentAccountResult.getPriorAccount());
    mAccount = currentAccountResult.getCurrentAccount();
    if (account != null)
    {
        //load data for new account
        getDataForAccount(account);
    }
}

// Sign out
if (app.signOut())
{
    clearDataForAccount(mAccount);
    mAccount = null;
}

多個帳戶公用用戶端應用程式

類別 MultipleAccountPublicClientApplication 是用來建立 MSAL 型應用程式,允許同時登入多個帳戶。 它可讓您取得、新增和移除帳戶,如下所示:

新增帳戶

在您的應用程式中使用一或多個帳戶,方法是呼叫 acquireToken 一或多次。

取得帳戶

  • 呼叫 getAccount 以取得特定帳戶。
  • 呼叫 getAccounts以取得應用程式目前已知的帳戶清單。

您的應用程式將無法列舉訊息代理程式應用程式已知裝置上的所有 Microsoft 身分識別平台 帳戶。 它只能列舉您的應用程式所使用的帳戶。 這些函式不會傳回已從裝置移除的帳戶。

拿掉帳戶

使用帳戶標識碼呼叫 removeAccount 來移除帳戶。

如果您的應用程式已設定為使用訊息代理程式,且已在裝置上安裝訊息代理程式,當您呼叫 removeAccount時,將不會從訊息代理程式中移除帳戶。 只會移除與客戶端相關聯的令牌。

多個帳戶案例

下列虛擬程式代碼示範如何建立多個帳戶應用程式、列出裝置上的帳戶,以及取得令牌。

// Construct Multiple Account Public Client Application
IMultipleAccountPublicClientApplication app = PublicClientApplication.createMultipleAccountPublicClientApplication(getApplicationContext(), R.raw.msal_config);

String[] scopes = {"User.Read"};
IAccount mAccount = null;

// Acquire a token interactively
// The user will be required to interact with a UI to obtain a token
app.acquireToken(getActivity(), scopes, new AuthenticationCallback()
 {

        @Override
        public void onSuccess(IAuthenticationResult authenticationResult) 
        {
            mAccount = authenticationResult.getAccount();
        }

        @Override
        public void onError(MsalException exception)
        {
        }

        @Override
        public void onCancel()
        {
        }
 });

...

// Get the default authority
String authority = app.getConfiguration().getDefaultAuthority().getAuthorityURL().toString();

// Get a list of accounts on the device
List<IAccount> accounts = app.getAccounts();

// Pick an account to obtain a token from without prompting the user to sign in
IAccount selectedAccount = accounts.get(0);

// Get a token without prompting the user
app.acquireTokenSilentAsync(scopes, selectedAccount, authority, new SilentAuthenticationCallback()
{

        @Override
        public void onSuccess(IAuthenticationResult authenticationResult) 
        {
            mAccount = authenticationResult.getAccount();
        }

        @Override
        public void onError(MsalException exception)
        {
        }
});