• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Authenticating a Domain Account (for System Applications Only)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: Account-->
5<!--Owner: @steven-q-->
6<!--Designer: @JiDong-CS1-->
7<!--Tester: @zhaimengchao-->
8<!--Adviser: @zengyawen-->
9
10Authenticate a domain account before unlocking the screen or when the login session fails.
11
12## Before You Start
13
14Import the **osAccount** module.
15
16```ts
17import { osAccount } from '@kit.BasicServicesKit';
18```
19
20## Domain Account Authentication by Password
21
22The domain account can be authenticated by password. You can use [auth](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#auth10) to implement this operation. To call this API, the application must have the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission.
23
24**Procedure**
25
261. Request the ohos.permission.ACCESS_USER_AUTH_INTERNAL permission. For details about how to request the permission, see [Requesting Permissions for system_basic Applications](../../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
27
282. Obtain user input information, including the domain account and its password.
29
30   ```ts
31     let domainAccountInfo: osAccount.DomainAccountInfo = {
32       domain: 'CHINA',
33       accountName: 'zhangsan'
34     }
35     let credential: Uint8Array = new Uint8Array([0]);
36   ```
37
383. Define the callback used to return the authentication result.
39
40   ```ts
41   let callback: osAccount.IUserAuthCallback = {
42     onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
43       console.log('auth resultCode = ' + resultCode);
44       console.log('auth authResult = ' + JSON.stringify(authResult));
45     }
46   }
47   ```
48
494. Use [auth](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#auth10) to authenticate the domain account by password.
50
51   ```ts
52   try {
53     osAccount.DomainAccountManager.auth(domainAccountInfo, credential, callback);
54   } catch (err) {
55     console.error('auth exception = ' + JSON.stringify(err));
56   }
57   ```
58
59## Domain Account Authentication by Dialog
60
61If the domain account password is unavailable, display a dialog box to authentication the domain account. You can use [authWithPopup](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#authwithpopup10) to implement this operation.
62
63**Procedure**
64
651. Define the callback used to return the authentication result.
66
67   ```ts
68   let callback: osAccount.IUserAuthCallback = {
69     onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
70       console.log('authWithPopup resultCode = ' + resultCode);
71       console.log('authWithPopup authResult = ' + JSON.stringify(authResult));
72     }
73   }
74   ```
75
762. Use [authWithPopup](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#authwithpopup10) to authenticate the domain account in a dialog box displayed.
77
78   ```ts
79   try {
80     osAccount.DomainAccountManager.authWithPopup(callback)
81   } catch (err) {
82     console.error('authWithPopup exception = ' + JSON.stringify(err));
83   }
84   ```
85