Authenticating Domain Accounts (for System Applications Only)
Authenticate a domain account before unlocking the screen or when the login session fails.
Getting Started
Import the osAccount module.
import { osAccount, BusinessError } from '@kit.BasicServicesKit';
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 application must have the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission.
Procedure
-
Request the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission. For details, see Requesting Permissions for system_basic Applications.
-
Obtain user input information, including the domain account and its password.
let domainAccountInfo: osAccount.DomainAccountInfo = {
domain: 'CHINA',
accountName: 'zhangsan'
};
let credential: Uint8Array = new Uint8Array([0]);
-
Define the callback used to return the authentication result.
let callback: osAccount.IUserAuthCallback = {
onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
console.info('auth resultCode = ' + resultCode);
console.info('auth authResult = ' + JSON.stringify(authResult));
// ...
}
};
-
Use auth to authenticate the domain account by password.
try {
osAccount.DomainAccountManager.auth(domainAccountInfo, credential, callback);
} catch (e) {
const err = e as BusinessError;
console.error(`auth exception = ${err.message}`);
}
Domain Account Authentication by 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
-
Define the callback used to return the authentication result.
let callback: osAccount.IUserAuthCallback = {
onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
console.info('authWithPopup resultCode = ' + resultCode);
console.info('authWithPopup authResult = ' + JSON.stringify(authResult));
// ...
}
}
-
Use authWithPopup to authenticate the domain account in a dialog box displayed.
try {
osAccount.DomainAccountManager.authWithPopup(callback)
} catch (e) {
const err = e as BusinessError;
console.error(`authWithPopup exception = ${err.message}`);
// ...
}