Authenticating a Domain Account

Authenticate a domain account before unlocking the screen or when the login session fails.

Before You Start

Import the osAccount module.

import account_osAccount from '@ohos.account.osAccount';

Domain Account Authentication by Password

The domain account can be authenticated by password. You can use auth to implement this operation. To call this API, the app must have the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission.

Procedure

  1. Request the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission. For details, see Requesting Permissions for system_basic Applications.

  2. Obtain user input information, including the domain account and its password.

      let domainAccountInfo: account_osAccount.DomainAccountInfo = {
        domain: 'CHINA',
        accountName: 'zhangsan'
      }
      let credential: Uint8Array = new Uint8Array([0]);
    
  3. Define the callback used to return the authentication result.

    let callback: IUserAuthCallback = {
      onResult: (resultCode: number, authResult: account_osAccount.AuthResult) => {
        console.log('auth resultCode = ' + resultCode);
        console.log('auth authResult = ' + JSON.stringify(authResult));
      }
    }
    
  4. Use auth to authenticate the domain account by password.

    try {
      account_osAccount.DomainAccountManager.auth(domainAccountInfo, credential, callback);
    } catch (err) {
      console.log('auth exception = ' + JSON.stringify(err));
    }
    

Domain Account Authentication by a Dialog

If the domain account password is unavailable, display a dialog box to authentication the domain account. You can use authWithPopup to implement this operation.

Procedure

  1. Define the callback used to return the authentication result.

    let callback: IUserCallback = {
      onResult: (resultCode: number, authResult: account_osAccount.AuthResult) => {
        console.log('authWithPopup resultCode = ' + resultCode);
        console.log('authWithPopup authResult = ' + JSON.stringify(authResult));
      }
    }
    
  2. Use authWithPopup to authenticate the domain account in a dialog box displayed.

    try {
      account_osAccount.DomainAccountManager.authWithPopup(callback)
    } catch (err) {
      console.log('authWithPopup exception = ' + JSON.stringify(err));
    }