分享方式:


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

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

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

MSAL 會將 PublicClientApplication 的 API 表面具體化,以簡化並釐清每次只允許使用一個帳戶的應用程式開發體驗。 PublicClientApplication 是由 SingleAccountPublicClientApplicationMultipleAccountPublicClientApplication 分割成子類別。 下圖顯示這些類別之間的關係。

SingleAccountPublicClientApplication UML 類別圖表

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

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

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

當裝置上已安裝 Android 驗證訊息代理程式 (例如 Microsoft Authenticator 或 Intune 公司入口網站),而且您的應用程式設定為使用訊息代理程式時,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)
        {
        }
});