@ohos.account.osAccount (系统账号管理)(系统接口)

本模块提供管理系统账号的基础能力,包括系统账号的添加、删除、查询、设置、订阅、启动等功能。

说明:

  • 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  • 当前页面仅包含本模块的系统接口,其他公开接口参见ohos.account.osAccount (系统账号管理)

导入模块

import { osAccount } from '@kit.BasicServicesKit';

AccountManager

系统账号管理类。

activateOsAccount

activateOsAccount(localId: number, callback: AsyncCallback<void>): void

激活指定系统账号。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
callback AsyncCallback<void> 回调函数。当账号激活成功时,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated.
12300016 The number of logged in accounts reaches the upper limit.

示例:

激活ID为100的系统账号。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.activateOsAccount(localId, (err: BusinessError)=>{
    if (err) {
      console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('activateOsAccount successfully');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`);
}

activateOsAccount

activateOsAccount(localId: number): Promise<void>

激活指定系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated.
12300016 The number of logged in accounts reaches the upper limit.

示例:

激活ID为100的系统账号。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.activateOsAccount(localId).then(() => {
    console.info('activateOsAccount successfully');
  }).catch((err: BusinessError) => {
    console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`activateOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

activateOsAccount23+

activateOsAccount(localId: number, displayId: number): Promise<void>

在指定逻辑屏激活(前台启动或切换)目标系统账号。使用Promise异步回调。

当前不支持跨逻辑屏激活,即在指定逻辑屏上激活另一个已在逻辑屏前台运行的系统账号。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
displayId number 逻辑屏ID。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见通用错误码账号管理错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
801 Capability not supported.
12300001 The system service works abnormally.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated.
12300016 The number of logged in accounts reaches the upper limit.
12300018 Display not found.
12300019 Cross-display activation not supported.

示例:

在ID为0的逻辑屏上激活ID为100的系统账号。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let displayId: number = 0;
try {
  accountManager.activateOsAccount(localId, displayId).then(() => {
    console.info('activateOsAccount with displayId successfully');
  }).catch((err: BusinessError) => {
    console.error(`activateOsAccount with displayId failed, err: ${err.code} ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`activateOsAccount with displayId exception: ${err.code} ${err.message}`);
}

deactivateOsAccount12+

deactivateOsAccount(localId: number): Promise<void>

注销(退出登录)指定系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated.

示例:

注销ID为100的系统账号。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.deactivateOsAccount(localId).then(() => {
    console.info('deactivateOsAccount successfully');
  }).catch((err: BusinessError) => {
    console.error(`deactivateOsAccount failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`deactivateOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

isOsAccountActivated11+

isOsAccountActivated(localId: number): Promise<boolean>

判断指定系统账号是否处于激活状态。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<boolean> Promise对象。返回true表示账号已激活;返回false表示账号未激活。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.

示例:

判断ID为100的系统账号是否处于激活状态。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.isOsAccountActivated(localId).then((isActivated: boolean) => {
    console.info('isOsAccountActivated successfully, isActivated: ' + isActivated);
  }).catch((err: BusinessError) => {
    console.error(`isOsAccountActivated failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`isOsAccountActivated exception: code is ${err.code}, message is ${err.message}`);
}

isOsAccountConstraintEnabled11+

isOsAccountConstraintEnabled(localId: number, constraint: string): Promise<boolean>

判断指定系统账号是否使能指定约束。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
constraint string 指定的约束名称。

返回值:

类型 说明
Promise<boolean> Promise对象。返回true表示已使能指定的约束;返回false表示未使能指定的约束。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.

示例:

判断ID为100的系统账号是否有禁止使用Wi-Fi的约束。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let constraint: string = 'constraint.wifi';
try {
  accountManager.isOsAccountConstraintEnabled(localId, constraint).then((isEnabled: boolean) => {
    console.info('isOsAccountConstraintEnabled successfully, isEnabled: ' + isEnabled);
  }).catch((err: BusinessError) => {
    console.error(`isOsAccountConstraintEnabled failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`isOsAccountConstraintEnabled exception: code is ${err.code}, message is ${err.message}`);
}

isOsAccountUnlocked11+

isOsAccountUnlocked(localId: number): Promise<boolean>

检查指定系统账号是否已验证。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。不填则检查当前系统账号是否已验证。

返回值:

类型 说明
Promise<boolean> Promise对象。返回true表示当前账号已认证解锁;返回false表示当前账号未认证解锁。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.isOsAccountUnlocked(localId).then((isVerified: boolean) => {
    console.info('isOsAccountUnlocked successfully, isVerified: ' + isVerified);
  }).catch((err: BusinessError) => {
    console.error(`isOsAccountUnlocked failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`isOsAccountUnlocked exception: code is ${err.code}, message is ${err.message}`);
}

removeOsAccount

removeOsAccount(localId: number, callback: AsyncCallback<void>): void

删除指定系统账号。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
callback AsyncCallback<void> 回调函数。如果删除账号成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated on.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let accountName: string = 'testAccountName';
try {
  accountManager.createOsAccount(accountName, osAccount.OsAccountType.NORMAL,
    (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo) => {
      accountManager.removeOsAccount(osAccountInfo.localId, (err: BusinessError)=>{
        if (err) {
          console.error(`removeOsAccount failed, code is ${err.code}, message is ${err.message}`);
        } else {
          console.info('removeOsAccount successfully');
        }
    });
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`removeOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

removeOsAccount

removeOsAccount(localId: number): Promise<void>

删除指定系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let accountName: string = 'testAccountName';
try {
  accountManager.createOsAccount(accountName, osAccount.OsAccountType.NORMAL,
    (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
      accountManager.removeOsAccount(osAccountInfo.localId).then(() => {
        console.info('removeOsAccount successfully');
      }).catch((err: BusinessError) => {
        console.error(`removeOsAccount failed, code is ${err.code}, message is ${err.message}`);
      });
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`removeOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

removeOsAccount24+

removeOsAccount(localId: number, options: RemoveOsAccountOptions): Promise<void>

根据删除选项,删除指定系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
options RemoveOsAccountOptions 删除系统账号的选项。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid localId or options.
12300003 Account not found.
12300008 Restricted Account.
12300010 Service busy. Possible causes: The target account is being operated on.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let accountName: string = 'testAccountName';
let token: Uint8Array = new Uint8Array([0]);
let options: osAccount.RemoveOsAccountOptions = {
  token: token,
}
try {
  accountManager.createOsAccount(accountName, osAccount.OsAccountType.NORMAL,
    (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
      accountManager.removeOsAccount(osAccountInfo.localId, options).then(() => {
        console.info('removeOsAccount successfully');
      }).catch((err: BusinessError) => {
        console.error(`removeOsAccount failed, code is ${err.code}, message is ${err.message}`);
      });
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`removeOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountConstraints

setOsAccountConstraints(localId: number, constraints: Array<string>, enable: boolean,callback: AsyncCallback<void>): void

为指定系统账号设置/删除约束。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
constraints Array<string> 待设置/删除的约束列表。
enable boolean 设置(true)/删除(false) 。
callback AsyncCallback<void> 回调函数。如果设置成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or constraints.
12300003 Account not found.
12300008 Restricted Account.

示例:

给ID为100的系统账号设置禁止使用Wi-Fi的约束。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let constraint: string = 'constraint.wifi';
try {
  accountManager.setOsAccountConstraints(localId, [constraint], true, (err: BusinessError) => {
    if (err) {
      console.error(`setOsAccountConstraints failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('setOsAccountConstraints successfully');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountConstraints exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountConstraints

setOsAccountConstraints(localId: number, constraints: Array<string>, enable: boolean): Promise<void>

为指定系统账号设置/删除约束。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
constraints Array<string> 待设置/删除的约束列表。
enable boolean 设置(true)/删除(false)。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or constraints.
12300003 Account not found.
12300008 Restricted Account.

示例:

删除ID为100的系统账号的禁止使用Wi-Fi的约束。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.setOsAccountConstraints(localId, ['constraint.location.set'], false).then(() => {
    console.info('setOsAccountConstraints successfully');
  }).catch((err: BusinessError) => {
    console.error(`setOsAccountConstraints failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountConstraints exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountName

setOsAccountName(localId: number, localName: string, callback: AsyncCallback<void>): void

设置指定系统账号的账号名。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
localName string 账号名,最大长度为1024个字符。
callback AsyncCallback<void> 回调函数。如果设置成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or localName.
12300003 Account not found.
12300008 Restricted Account.

示例:

将ID为100的系统账号的账号名设置成demoName。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let name: string = 'demoName';
try {
  accountManager.setOsAccountName(localId, name, (err: BusinessError) => {
    if (err) {
      console.error(`setOsAccountName failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('setOsAccountName successfully');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountName exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountName

setOsAccountName(localId: number, localName: string): Promise<void>

设置指定系统账号的账号名。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
localName string 账号名,最大长度为1024个字符。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or localName.
12300003 Account not found.
12300008 Restricted Account.

示例:

将ID为100的系统账号的账号名设置成demoName。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let name: string = 'testName';
try {
  accountManager.setOsAccountName(localId, name).then(() => {
    console.info('setOsAccountName successfully');
  }).catch((err: BusinessError) => {
    console.error(`setOsAccountName failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountName exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountType24+

setOsAccountType(localId: number, type: OsAccountType, options?: SetOsAccountTypeOptions): Promise<void>

设置指定系统账号的账号类型。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
type OsAccountType 系统账号类型。
options SetOsAccountTypeOptions 设置系统账号类型的选项。默认为空。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes: 1. The operation is restricted by the OS-account constraint. 2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid type or options.
12300003 Account not found.
12300008 Restricted OS account.
12300010 Service busy. Possible causes: The target account is being operated.
12300023 The number of accounts of the specified type has reached the upper limit.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let type: osAccount.OsAccountType = osAccount.OsAccountType.ADMIN;
let options: osAccount.SetOsAccountTypeOptions = {
  token: new Uint8Array([0, 1, 2, 3])
};
try {
  accountManager.setOsAccountType(localId, type, options).then(() => {
    console.info('setOsAccountType successfully');
  }).catch((err: BusinessError) => {
    console.error(`setOsAccountType failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountType exception: code is ${err.code}, message is ${err.message}`);
}

queryMaxOsAccountNumber

queryMaxOsAccountNumber(callback: AsyncCallback<number>): void

查询允许创建的系统账号的最大数量。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
callback AsyncCallback<number> 回调函数,如果查询成功,err为null,data为允许创建的系统账号的最大数量;否则为错误对象。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryMaxOsAccountNumber((err: BusinessError, maxCnt: number) => {
    if (err) {
      console.error(`queryMaxOsAccountNumber failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('queryMaxOsAccountNumber successfully, maxCnt:' + maxCnt);
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryMaxOsAccountNumber exception: code is ${err.code}, message is ${err.message}`);
}

queryMaxOsAccountNumber

queryMaxOsAccountNumber(): Promise<number>

查询允许创建的系统账号的最大数量。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

返回值:

类型 说明
Promise<number> Promise对象,返回允许创建的系统账号的最大数量。

错误码:

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryMaxOsAccountNumber().then((maxCnt: number) => {
    console.info('queryMaxOsAccountNumber successfully, maxCnt: ' + maxCnt);
  }).catch((err: BusinessError) => {
    console.error(`queryMaxOsAccountNumber failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryMaxOsAccountNumber exception: code is ${err.code}, message is ${err.message}`);
}

queryMaxLoggedInOsAccountNumber12+

queryMaxLoggedInOsAccountNumber(): Promise<number>

查询允许同时登录的系统账号的最大数量。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

返回值:

类型 说明
Promise<number> Promise对象,返回允许登录的系统账号的最大数量。

错误码:

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryMaxLoggedInOsAccountNumber().then((maxNum: number) => {
    console.info('queryMaxLoggedInOsAccountNumber successfully, maxNum: ' + maxNum);
  }).catch((err: BusinessError) => {
    console.error(`queryMaxLoggedInOsAccountNumber failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryMaxLoggedInOsAccountNumber exception: code is ${err.code}, message is ${err.message}`);
}

getEnabledOsAccountConstraints11+

getEnabledOsAccountConstraints(localId: number): Promise<Array<string>>

获取指定系统账号已使能的全部约束。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<Array<string>> Promise对象,返回指定系统账号已使能的全部约束

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.

示例:

获取ID为100的系统账号的全部约束。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.getEnabledOsAccountConstraints(localId).then((constraints: string[]) => {
    console.info('getEnabledOsAccountConstraints, constraints: ' + constraints);
  }).catch((err: BusinessError) => {
    console.error(`getEnabledOsAccountConstraints err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getEnabledOsAccountConstraints exception: code is ${err.code}, message is ${err.message}`);
}

queryAllCreatedOsAccounts

queryAllCreatedOsAccounts(callback: AsyncCallback<Array<OsAccountInfo>>): void

查询已创建的所有系统账号的信息列表。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
callback AsyncCallback<Array<OsAccountInfo>> 回调函数。如果查询成功,err为null,data为已创建的所有系统账号的信息列表;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryAllCreatedOsAccounts((err: BusinessError, accountArr: osAccount.OsAccountInfo[])=>{
    if (err) {
      console.error(`queryAllCreatedOsAccounts exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('queryAllCreatedOsAccounts accountArr:' + JSON.stringify(accountArr));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryAllCreatedOsAccounts exception: code is ${err.code}, message is ${err.message}`);
}

queryAllCreatedOsAccounts

queryAllCreatedOsAccounts(): Promise<Array<OsAccountInfo>>

查询已创建的所有系统账号的信息列表。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

返回值:

类型 说明
Promise<Array<OsAccountInfo>> Promise对象,返回已创建的所有系统账号的信息列表。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryAllCreatedOsAccounts().then((accountArr: osAccount.OsAccountInfo[]) => {
    console.info('queryAllCreatedOsAccounts, accountArr: ' + JSON.stringify(accountArr));
  }).catch((err: BusinessError) => {
    console.error(`queryAllCreatedOsAccounts err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryAllCreatedOsAccounts exception: code is ${err.code}, message is ${err.message}`);
}

createOsAccount

createOsAccount(localName: string, type: OsAccountType, callback: AsyncCallback<OsAccountInfo>): void

创建一个系统账号。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localName string 创建的系统账号的名称。
type OsAccountType 创建的系统账号的类型。
callback AsyncCallback<OsAccountInfo> 回调函数。如果创建成功,err为null,data为新创建的系统账号的信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid localName or type.
12300004 Local name already exists.
12300005 Multi-user not supported.
12300006 Unsupported account type.
12300007 The number of accounts has reached the upper limit.
12300023 The number of accounts of the specified type has reached the upper limit.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.createOsAccount('testName', osAccount.OsAccountType.NORMAL,
    (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
    if (err) {
      console.error(`createOsAccount exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('createOsAccount osAccountInfo:' + JSON.stringify(osAccountInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`createOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

createOsAccount

createOsAccount(localName: string, type: OsAccountType, options?: CreateOsAccountOptions): Promise<OsAccountInfo>

创建一个系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localName string 创建的系统账号的名称。
type OsAccountType 创建的系统账号的类型。
options CreateOsAccountOptions 创建系统账号的选项,默认为空。
从API version 12开始支持该可选参数。

返回值:

类型 说明
Promise<OsAccountInfo> Promise对象,返回新创建的系统账号的信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
12300001 The system service works abnormally.
12300002 Invalid localName, type or options.
12300004 Local name already exists.
12300005 Multi-user not supported.
12300006 Unsupported account type.
12300007 The number of accounts has reached the upper limit.
12300015 The short name already exists.
12300023 The number of accounts of the specified type has reached the upper limit.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let options: osAccount.CreateOsAccountOptions = {
  shortName: 'myShortName',
  disallowedPreinstalledBundles: [],
  allowedPreinstalledBundles: [],
}
try {
  accountManager.createOsAccount('testAccountName', osAccount.OsAccountType.NORMAL, options).then(
    (accountInfo: osAccount.OsAccountInfo) => {
    console.info('createOsAccount, accountInfo: ' + JSON.stringify(accountInfo));
  }).catch((err: BusinessError) => {
    console.error(`createOsAccount err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`createOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

createOsAccountForDomain8+

createOsAccountForDomain(type: OsAccountType, domainInfo: DomainAccountInfo, callback: AsyncCallback<OsAccountInfo>): void

根据域账号信息,创建一个系统账号并将其与域账号关联。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type OsAccountType 创建的系统账号的类型。
domainInfo DomainAccountInfo 域账号信息。
callback AsyncCallback<OsAccountInfo> 回调函数。如果创建成功,err为null,data为新创建的系统账号的信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid type or domainInfo.
12300004 Account already exists.
12300005 Multi-user not supported.
12300006 Unsupported account type.
12300007 The number of accounts has reached the upper limit.
12300023 The number of accounts of the specified type has reached the upper limit.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let domainInfo: osAccount.DomainAccountInfo =
  {domain: 'testDomain', accountName: 'testAccountName'};
try {
  accountManager.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo,
    (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
    if (err) {
      console.error(`createOsAccountForDomain exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`createOsAccountForDomain exception: code is ${err.code}, message is ${err.message}`);
}

createOsAccountForDomain8+

createOsAccountForDomain(type: OsAccountType, domainInfo: DomainAccountInfo, options?: CreateOsAccountForDomainOptions): Promise<OsAccountInfo>

根据传入的域账号信息,创建与其关联的系统账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type OsAccountType 创建的系统账号的类型。
domainInfo DomainAccountInfo 域账号信息。
options CreateOsAccountForDomainOptions 创建账号的可选参数,默认为空。
从API version 12开始支持该可选参数。

返回值:

类型 说明
Promise<OsAccountInfo> Promise对象,返回新创建的系统账号的信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
204 Access denied due to user access control policy. Possible causes:
1. The operation is restricted by the OS-account constraint.
2. The required privilege for the operation has not been granted.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid type, domainInfo or options.
12300004 Account already exists.
12300005 Multi-user not supported.
12300006 Unsupported account type.
12300007 The number of accounts has reached the upper limit.
12300015 The short name already exists.
12300023 The number of accounts of the specified type has reached the upper limit.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let domainInfo: osAccount.DomainAccountInfo =
  {domain: 'testDomain', accountName: 'testAccountName'};
let options: osAccount.CreateOsAccountForDomainOptions = {
  shortName: 'myShortName'
}
try {
  accountManager.createOsAccountForDomain(osAccount.OsAccountType.NORMAL, domainInfo, options).then(
    (accountInfo: osAccount.OsAccountInfo) => {
    console.info('createOsAccountForDomain, account info: ' + JSON.stringify(accountInfo));
  }).catch((err: BusinessError) => {
    console.error(`createOsAccountForDomain err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`createOsAccountForDomain exception: code is ${err.code}, message is ${err.message}`);
}

queryOsAccount11+

queryOsAccount(): Promise<OsAccountInfo>

查询当前进程所属的系统账号的信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.GET_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

返回值:

类型 说明
Promise<OsAccountInfo> Promise对象,返回当前进程所属的系统账号信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.queryOsAccount().then((accountInfo: osAccount.OsAccountInfo) => {
    console.info('queryOsAccount, accountInfo: ' + JSON.stringify(accountInfo));
  }).catch((err: BusinessError) => {
    console.error(`queryOsAccount err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

queryOsAccountById

queryOsAccountById(localId: number, callback: AsyncCallback<OsAccountInfo>): void

查询指定系统账号的信息。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号的ID。
callback AsyncCallback<OsAccountInfo> 回调函数。如果查询成功,err为null,data为查到的系统账号的信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.

示例:

查询ID为100的系统账号信息。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.queryOsAccountById(localId, (err: BusinessError, accountInfo: osAccount.OsAccountInfo)=>{
    if (err) {
      console.error(`queryOsAccountById exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('queryOsAccountById accountInfo:' + JSON.stringify(accountInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryOsAccountById exception: code is ${err.code}, message is ${err.message}`);
}

queryOsAccountById

queryOsAccountById(localId: number): Promise<OsAccountInfo>

查询指定系统账号的信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号的ID。

返回值:

类型 说明
Promise<OsAccountInfo> Promise对象,返回查到的系统账号的信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.

示例:

查询ID为100的系统账号信息。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.queryOsAccountById(localId).then((accountInfo: osAccount.OsAccountInfo) => {
    console.info('queryOsAccountById, accountInfo: ' + JSON.stringify(accountInfo));
  }).catch((err: BusinessError) => {
    console.error(`queryOsAccountById err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`queryOsAccountById exception: code is ${err.code}, message is ${err.message}`);
}

getOsAccountProfilePhoto

getOsAccountProfilePhoto(localId: number, callback: AsyncCallback<string>): void

获取指定系统账号的头像信息。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
callback AsyncCallback<string> 回调函数。如果获取成功,err为null,data为指定系统账号的头像信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.

示例:

获取ID为100的系统账号的头像。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.getOsAccountProfilePhoto(localId, (err: BusinessError, photo: string)=>{
    if (err) {
      console.error(`getOsAccountProfilePhoto exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('get photo:' + photo + ' by localId: ' + localId);
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountProfilePhoto exception: code is ${err.code}, message is ${err.message}`);
}

getOsAccountProfilePhoto

getOsAccountProfilePhoto(localId: number): Promise<string>

获取指定系统账号的头像信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<string> Promise对象,返回指定系统账号的头像信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 Account not found.

示例:

获取ID为100的系统账号的头像。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.getOsAccountProfilePhoto(localId).then((photo: string) => {
    console.info('getOsAccountProfilePhoto: ' + photo);
  }).catch((err: BusinessError) => {
    console.error(`getOsAccountProfilePhoto err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountProfilePhoto exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountProfilePhoto

setOsAccountProfilePhoto(localId: number, photo: string, callback: AsyncCallback<void>): void

为指定系统账号设置头像信息。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
photo string 头像信息。
callback AsyncCallback<void> 回调函数。如果设置成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or photo.
12300003 Account not found.
12300008 Restricted Account.

示例:

给ID为100的系统账号设置头像。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let photo: string = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAPCAYAAAA/I0V3AAAAAXNSR0IArs4c6QAAAARnQU1BAA'+
'Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACwSURBVDhPvZLBDYMwDEV/ugsXRjAT0EHCOuFIBwkbdIRewi6unbiAyoGgSn1SFH85+Y'+
'q/4ljARW62X+LHS8uIzjm4dXUYF+utzBikB52Jo5e5iEPKqpACk7R9NM2RvWm5tIkD2czLCUFNKLD6IjdMHFHDzws285MgGrT0xCtp3WOKHo'+
'+7q0mP0DZW9pNmoEFUzrQjp5cCnaen2kSJXLFD8ghbXyZCMQf/8e8Ns1XVAG/XAgqKzVnJFAAAAABJRU5ErkJggg=='
try {
  accountManager.setOsAccountProfilePhoto(localId, photo, (err: BusinessError)=>{
    if (err) {
      console.error(`setOsAccountProfilePhoto exception:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('setOsAccountProfilePhoto successful.');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountProfilePhoto exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountProfilePhoto

setOsAccountProfilePhoto(localId: number, photo: string): Promise<void>

为指定系统账号设置头像信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。
photo string 头像信息。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid localId or photo.
12300003 Account not found.
12300008 Restricted Account.

示例:

给ID为100的系统账号设置头像。

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
let photo: string = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAPCAYAAAA/I0V3AAAAAXNSR0IArs4c6QAAAARnQU1BAA'+
'Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACwSURBVDhPvZLBDYMwDEV/ugsXRjAT0EHCOuFIBwkbdIRewi6unbiAyoGgSn1SFH85+Y'+
'q/4ljARW62X+LHS8uIzjm4dXUYF+utzBikB52Jo5e5iEPKqpACk7R9NM2RvWm5tIkD2czLCUFNKLD6IjdMHFHDzws285MgGrT0xCtp3WOKHo'+
'+7q0mP0DZW9pNmoEFUzrQjp5cCnaen2kSJXLFD8ghbXyZCMQf/8e8Ns1XVAG/XAgqKzVnJFAAAAABJRU5ErkJggg=='
try {
  accountManager.setOsAccountProfilePhoto(localId, photo).then(() => {
    console.info('setOsAccountProfilePhoto success');
  }).catch((err: BusinessError) => {
    console.error(`setOsAccountProfilePhoto err: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountProfilePhoto exception: code is ${err.code}, message is ${err.message}`);
}

on

on(type: 'activate' | 'activating', name: string, callback: Callback<number>): void

订阅系统账号的激活完成与激活中的事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'activate' | 'activating' 订阅类型,activate表示订阅的是账号已激活完成的事件,activating表示订阅的是账号正在激活的事件。
name string 订阅名称,可自定义,要求非空且长度不超过1024字节。
callback Callback<number> 订阅系统账号激活完成与激活中的事件回调,表示激活完成后或正在激活中的系统账号ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid type or name.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();

function onCallback(receiveLocalId: number){
  console.info('receive localId:' + receiveLocalId);
}

try {
  accountManager.on('activating', 'osAccountOnOffNameA', onCallback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`receive localId exception: code is ${err.code}, message is ${err.message}`);
}

off

off(type: 'activate' | 'activating', name: string, callback?: Callback<number>): void

取消订阅系统账号的激活完成与激活中的事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'activate' | 'activating' 取消订阅类型,activate表示取消订阅账号已激活完成的事件,activating取消订阅账号正在激活的事件。
name string 订阅名称,可自定义,要求非空且长度不超过1024字节,需要与订阅接口传入的值保持一致。
callback Callback<number> 取消订阅系统账号激活完成与激活中的事件回调,默认为空,表示取消该类型事件的所有回调。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid type or name.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();

function offCallback(){
  console.info('off enter')
}

try {
  accountManager.off('activating', 'osAccountOnOffNameA', offCallback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`off exception: code is ${err.code}, message is ${err.message}`);
}

on12+

on(type: 'switching', callback: Callback<OsAccountSwitchEventData>): void

订阅系统账号的前后台正在切换事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

从API version 23开始,权限ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS开始生效。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'switching' 订阅类型,switching表示订阅的是系统账号的前后台正在切换事件。
callback Callback<OsAccountSwitchEventData> 订阅系统账号的前后台正在切换事件回调,包含切换来源和切换目标的系统账号ID。
说明: 从API version 23开始,事件数据中新增可选字段displayId,表示发生切换事件的逻辑屏ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid type.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();

function onSwitchingCallback(eventData: osAccount.OsAccountSwitchEventData){
  console.info('receive eventData:' + JSON.stringify(eventData));
}

try {
  accountManager.on('switching', onSwitchingCallback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`receive eventData exception: code is ${err.code}, message is ${err.message}`);
}

off12+

off(type: 'switching', callback?: Callback<OsAccountSwitchEventData>): void

取消订阅系统账号的前后台正在切换事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

从API version 23开始,权限ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS开始生效。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'switching' 取消订阅类型,switching表示取消订阅的是系统账号的前后台正在切换事件。
callback Callback<OsAccountSwitchEventData> 取消订阅系统账号的前后台正在切换事件回调,默认为空,表示取消该类型事件的所有回调。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid type.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.off('switching');
} catch (e) {
  const err = e as BusinessError;
  console.error(`off exception: code is ${err.code}, message is ${err.message}`);
}

on12+

on(type: 'switched', callback: Callback<OsAccountSwitchEventData>): void

订阅系统账号的前后台切换结束事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

从API version 23开始,权限ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS开始生效。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'switched' 订阅类型,switched表示订阅的是系统账号的前后台切换结束事件。
callback Callback<OsAccountSwitchEventData> 订阅系统账号的前后台切换结束事件回调,包含切换来源和切换目标的系统账号ID。
说明: 从API version 23开始,事件数据中新增可选字段displayId,表示发生切换事件的逻辑屏ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid type.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();

function onSwitchedCallback(eventData: osAccount.OsAccountSwitchEventData){
  console.info('receive eventData:' + JSON.stringify(eventData));
}

try {
  accountManager.on('switched', onSwitchedCallback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`receive eventData exception: code is ${err.code}, message is ${err.message}`);
}

off12+

off(type: 'switched', callback?: Callback<OsAccountSwitchEventData>): void

取消订阅系统账号的前后台切换结束事件。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

从API version 23开始,权限ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS开始生效。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
type 'switched' 取消订阅类型,switched表示取消订阅的是系统账号的前后台切换结束事件。
callback Callback<OsAccountSwitchEventData> 取消订阅系统账号的前后台切换结束事件回调,默认为空,表示取消该类型事件的所有回调。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid type.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.off('switched');
} catch (e) {
  const err = e as BusinessError;
  console.error(`off exception: code is ${err.code}, message is ${err.message}`);
}

onConstraintChanged23+

onConstraintChanged(constraints: string[], callback: Callback<ConstraintChangeInfo>): void

订阅调用方所属系统账号的一种或多种约束变更事件。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
constraints string[] 表示待订阅的约束列表。
callback Callback<ConstraintChangeInfo> 表示用于接收约束变更事件的回调函数。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.
12300002 One or more constraints are invalid.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let constraint: string = 'constraint.wifi';
const callback:Callback<osAccount.ConstraintChangeInfo> = (data: osAccount.ConstraintChangeInfo): void => {
  console.info(`ConstraintChangeInfo received, constraint: ${data.constraint} isEnabled: ${data.isEnabled}`);
};

try {
  accountManager.onConstraintChanged([constraint], callback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`onConstraintChanged exception: code is ${err.code}, message is ${err.message}`);
}

offConstraintChanged23+

offConstraintChanged(callback?: Callback<ConstraintChangeInfo>): void

取消与指定回调关联的约束变更订阅记录。若未指定回调,则取消所有订阅记录。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
callback Callback<ConstraintChangeInfo> 表示用于接收约束变更事件的回调函数。
默认为undefined,表示清除所有订阅记录。
非undefined时,表示清除与该回调函数关联的订阅记录。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let constraint: string = 'constraint.wifi';
const callback:Callback<osAccount.ConstraintChangeInfo> = (data: osAccount.ConstraintChangeInfo): void => {
  console.info(`ConstraintChangeInfo received, constraint: ${data.constraint} isEnabled: ${data.isEnabled}`);
};

try {
  accountManager.onConstraintChanged([constraint], callback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`onConstraintChanged exception: code is ${err.code}, message is ${err.message}`);
}

try {
  accountManager.offConstraintChanged(callback);
} catch (e) {
  const err = e as BusinessError;
  console.error(`offConstraintChanged exception: code is ${err.code}, message is ${err.message}`);
}

getBundleIdForUid9+

getBundleIdForUid(uid: number, callback: AsyncCallback<number>): void

通过uid查询对应的bundleId。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
uid number 进程uid。
callback AsyncCallback<number> 回调函数。如果查询成功,err为null,data为与uid对应的bundleId;否则为错误对象。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid uid.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let testUid: number = 1000000;
try {
  accountManager.getBundleIdForUid(testUid, (err: BusinessError, bundleId: number) => {
    if (err) {
      console.error(`getBundleIdForUid errInfo:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getBundleIdForUid bundleId:' + JSON.stringify(bundleId));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getBundleIdForUid exception: code is ${err.code}, message is ${err.message}`);
}

getBundleIdForUid9+

getBundleIdForUid(uid: number): Promise<number>

通过uid查询对应的bundleId。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
uid number 进程uid。

返回值:

类型 说明
Promise<number> Promise对象,返回与uid对应的bundleId。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid uid.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let testUid: number = 1000000;
try {
  accountManager.getBundleIdForUid(testUid).then((result: number) => {
    console.info('getBundleIdForUid bundleId:' + JSON.stringify(result));
  }).catch((err: BusinessError) => {
    console.error(`getBundleIdForUid errInfo:code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getBundleIdForUid exception: code is ${err.code}, message is ${err.message}`);
}

getBundleIdForUidSync10+

getBundleIdForUidSync(uid: number): number

通过uid查询对应的bundleId。使用同步方式返回结果。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
uid number 进程uid。

返回值:

类型 说明
number 表示与进程uid对应的bundleId。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300002 Invalid uid.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let testUid: number = 1000000;
try {
  let bundleId : number = accountManager.getBundleIdForUidSync(testUid);
  console.info('getBundleIdForUidSync bundleId:' + bundleId);
} catch (e) {
  const err = e as BusinessError;
  console.error(`getBundleIdForUidSync exception: code is ${err.code}, message is ${err.message}`);
}

isMainOsAccount9+

isMainOsAccount(callback: AsyncCallback<boolean>): void

查询当前进程是否处于主用户。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
callback AsyncCallback<boolean> 回调函数,返回true表示当前账号为主账号,返回false表示当前账号非主账号。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.isMainOsAccount((err: BusinessError,result: boolean)=>{
    if (err) {
      console.error(`isMainOsAccount errInfo:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('isMainOsAccount result:' + JSON.stringify(result));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`isMainOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

isMainOsAccount9+

isMainOsAccount(): Promise<boolean>

查询当前进程是否处于主用户。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

返回值:

类型 说明
Promise<boolean> Promise对象,返回true表示当前账号为主账号,返回false表示当前账号非主账号。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.isMainOsAccount().then((result: boolean) => {
    console.info('isMainOsAccount result:' + JSON.stringify(result));
  }).catch((err: BusinessError) => {
    console.error(`isMainOsAccount errInfo:code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`isMainOsAccount exception: code is ${err.code}, message is ${err.message}`);
}

getForegroundOsAccountLocalId23+

getForegroundOsAccountLocalId(displayId: number): Promise<number>

获取指定逻辑屏上运行的前台系统账号ID。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
displayId number 逻辑屏ID。

返回值:

类型 说明
Promise<number> Promise对象,返回系统账号ID。

错误码:

以下错误码的详细介绍请参见通用错误码账号管理错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300017 The foreground OS account is not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let displayId: number = 0;
try {
  accountManager.getForegroundOsAccountLocalId(displayId).then((localId: number) => {
    console.info('foreground account on display ' + displayId + ' is ' + localId);
  }).catch((err: BusinessError) => {
    console.error(`getForegroundOsAccountLocalId failed: ${err.code} ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getForegroundOsAccountLocalId exception: ${err.code} ${err.message}`);
}

getForegroundOsAccountDisplayId23+

getForegroundOsAccountDisplayId(localId: number): Promise<number>

获取指定前台系统账号所运行的逻辑屏ID。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 系统账号ID。

返回值:

类型 说明
Promise<number> Promise对象,返回逻辑屏ID。

错误码:

以下错误码的详细介绍请参见通用错误码账号管理错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300017 The foreground OS account is not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
let localId: number = 100;
try {
  accountManager.getForegroundOsAccountDisplayId(localId).then((displayId: number) => {
    console.info('account ' + localId + ' foreground displayId: ' + displayId);
  }).catch((err: BusinessError) => {
    console.error(`getForegroundOsAccountDisplayId failed: ${err.code} ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getForegroundOsAccountDisplayId exception: ${err.code} ${err.message}`);
}

getOsAccountConstraintSourceTypes9+

getOsAccountConstraintSourceTypes(localId: number, constraint: string, callback: AsyncCallback<Array<ConstraintSourceTypeInfo>>): void

查询指定系统账号的指定约束来源信息。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号ID。
constraint string 要查询的约束名称。
callback AsyncCallback<Array<ConstraintSourceTypeInfo>> 回调函数。如果成功,err为null,data为指定系统账号的指定约束来源信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid name or constraint.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.getOsAccountConstraintSourceTypes(100, 'constraint.wifi',
    (err: BusinessError,sourceTypeInfos: osAccount.ConstraintSourceTypeInfo[])=>{
    if (err) {
      console.error(`getOsAccountConstraintSourceTypes errInfo:code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getOsAccountConstraintSourceTypes sourceTypeInfos:' + JSON.stringify(sourceTypeInfos));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountConstraintSourceTypes exception: code is ${err.code}, message is ${err.message}`);
}

getOsAccountConstraintSourceTypes9+

getOsAccountConstraintSourceTypes(localId: number, constraint: string): Promise<Array<ConstraintSourceTypeInfo>>

查询指定系统账号的指定约束来源信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号ID。
constraint string 要查询的约束名称。

返回值:

类型 说明
Promise<Array<ConstraintSourceTypeInfo>> Promise对象,返回指定系统账号的指定约束来源信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid name or constraint.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  accountManager.getOsAccountConstraintSourceTypes(100, 'constraint.wifi').then(
    (result: osAccount.ConstraintSourceTypeInfo[]) => {
    console.info('getOsAccountConstraintSourceTypes sourceTypeInfos:' + JSON.stringify(result));
  }).catch((err: BusinessError) => {
    console.error(`getOsAccountConstraintSourceTypes errInfo:code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountConstraintSourceTypes exception: code is ${err.code}, message is ${err.message}`);
}

getOsAccountType12+

getOsAccountType(localId: number): Promise<OsAccountType>

查询指定系统账号的类型。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号ID。

返回值:

类型 说明
Promise<OsAccountType> Promise对象,返回指定系统账号的类型。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  let localId: number = 100;
  accountManager.getOsAccountType(localId).then((type: osAccount.OsAccountType) => {
    console.info('getOsAccountType Type:' + type);
  }).catch((err: BusinessError) => {
    console.error(`getOsAccountType errInfo:code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountType exception: code is ${err.code}, message is ${err.message}`);
}

bindDomainAccount20+

bindDomainAccount(localId: number, domainAccountInfo: DomainAccountInfo): Promise<void>

在指定系统账号上绑定指定域账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
localId number 要查询的系统账号ID。
domainAccountInfo DomainAccountInfo 域账号信息。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见通用通用错误码账号管理错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid domain account information.
12300003 The OS account not found.
12300008 Restricted OS account. Possible causes: The OS account cannot be bound.
12300010 Service busy. Possible causes: The target OS account or domain account is being operated.
12300021 The OS account is already bound.
12300022 The domain account is already bound.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
try {
  let localId: number = 100;
  let domainInfo: osAccount.DomainAccountInfo =
    { domain: 'testDomain', accountName: 'testAccountName' };
  accountManager.bindDomainAccount(localId, domainInfo).then(() => {
    console.info('bindDomainAccount success.');
  }).catch((error: BusinessError) => {
    console.error(`bindDomainAccount failed, errCode=${error.code}, errMsg=${error.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`bindDomainAccount error, errCode=${error.code}, errMsg=${error.message}`);
}

osAccount.getAuthorizationManager24+

getAuthorizationManager(): AuthorizationManager

获取系统账号授权管理器。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

返回值:

类型 说明
AuthorizationManager 返回系统账号授权管理的实例对象。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
202 Not system application.

示例:

let authorizationManager: osAccount.AuthorizationManager = osAccount.getAuthorizationManager();

AuthorizationManager24+

系统账号授权管理类,用于管理系统账号授权。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

acquireAuthorization24+

acquireAuthorization(privilege: string, options?: AcquireAuthorizationOptions): Promise<AcquireAuthorizationResult>

为当前进程获取授权。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.ACQUIRE_LOCAL_ACCOUNT_AUTHORIZATION

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名 类型 必填 说明
privilege string 目标权限,详见配置文件
options AcquireAuthorizationOptions 获取授权的选项,默认为空。

返回值:

类型 说明
Promise<AcquireAuthorizationResult> Promise对象,返回获取授权的结果。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid privilege or options.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let authorizationManager: osAccount.AuthorizationManager = osAccount.getAuthorizationManager();
let privilege: string = 'testPrivilege';
let options: osAccount.AcquireAuthorizationOptions = {
  challenge: new Uint8Array([1, 2, 3]),
  isReuseNeeded: true,
  isInteractionAllowed: true,
};
try {
  authorizationManager.acquireAuthorization(privilege, options).then((result: osAccount.AcquireAuthorizationResult) => {
    console.info(`acquireAuthorization successfully, resultCode: ${result.resultCode}`);
  }).catch((err: BusinessError) => {
    console.error(`acquireAuthorization failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`acquireAuthorization exception: code is ${err.code}, message is ${err.message}`);
}

hasAuthorization24+

hasAuthorization(privilege: string): Promise<boolean>

检查当前进程是否已获得指定特权的授权。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名 类型 必填 说明
privilege string 目标权限,详见配置文件

返回值:

类型 说明
Promise<boolean> Promise对象,返回true表示已获得指定特权的授权;返回false表示未获得指定特权的授权。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid privilege.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let authorizationManager: osAccount.AuthorizationManager = osAccount.getAuthorizationManager();
let privilege: string = 'testPrivilege';

try {
  authorizationManager.hasAuthorization(privilege).then((isAuthorized: boolean) => {
    console.info(`Privilege: ${privilege} has been authorized: ${isAuthorized}`);
  }).catch((e:Error) => {
    const err = e as BusinessError;
    console.error(`hasAuthorization failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`hasAuthorization exception: code is ${err.code}, message is ${err.message}`);
}

releaseAuthorization24+

releaseAuthorization(privilege: string): Promise<void>

为当前进程撤销指定特权的授权。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名 类型 必填 说明
privilege string 目标权限,详见配置文件

返回值:

类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid privilege.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let authorizationManager: osAccount.AuthorizationManager = osAccount.getAuthorizationManager();
let privilege: string = 'testPrivilege';

try {
  authorizationManager.releaseAuthorization(privilege).then(() => {
    console.info('releaseAuthorization success');
  }).catch((e:Error) => {
    const err = e as BusinessError;
    console.error(`releaseAuthorization failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`releaseAuthorization exception: code is ${err.code}, message is ${err.message}`);
}

AcquireAuthorizationOptions24+

表示获取授权的选项。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

名称 类型 只读 可选 说明
challenge Uint8Array 随机挑战值,可用于防止重放攻击,长度不得超过32字节,默认为undefined。
isReuseNeeded boolean 是否需要重复用先前的授权,默认为true。
如果为true且存在有效的授权结果,则将复用该结果;否则,将执行新的授权。
isInteractionAllowed boolean 是否允许用户交互,默认为true 。
如果为true,则允许在交互上下文中显示授权对话框;如果为false,则不允许显示授权对话框。
注意:此选项仅在调用者位于前台时生效。如果调用者在后台,则不允许用户交互。
interactionContext Context 用户交互上下文配置,默认为undefined。
- 未指定上下文时,授权对话框以模态系统模式显示。
- 指定UIAbilityContextUIExtensionContext时,以模态应用模式显示。
- 未提供有效上下文时,授权对话框无法显示。
注意:仅当isInteractionAllowed为true时生效。

AuthorizationResultCode24+

表示授权结果码的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

名称 说明
AUTHORIZATION_SUCCESS 0 表示授权成功。
AUTHORIZATION_CANCELED 12300301 表示授权已取消。
AUTHORIZATION_INTERACTION_NOT_ALLOWED 12300302 表示服务因不允许用户交互而拒绝授权。
可能原因:
1. 调用者位于后台;
2. isInteractionAllowed选项的值为false;
3. 指定的交互上下文无效。
AUTHORIZATION_DENIED 12300303 表示因不符合授权规则,如账号类型不是管理员、设备类型不支持等原因而拒绝授权。
AUTHORIZATION_SERVICE_BUSY 12300304 表示服务忙碌。
可能原因:正在处理其他授权。

AcquireAuthorizationResult24+

表示获取授权的结果。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

模型约束: 此接口仅可在Stage模型下使用。

名称 类型 只读 可选 说明
resultCode AuthorizationResultCode 授权结果码。
privilege string 与授权关联的权限。
isReused boolean 是否为复用的授权结果,默认为undefined。
true:表示是复用的授权结果。false:表示不是复用的授权结果。
validityPeriod number 授权的有效期,默认值为300,单位为s。
token Uint8Array 授权令牌,默认为undefined。

UserAuth8+

用户认证类。

系统接口: 此接口为系统接口。

constructor8+

constructor()

创建用户认证的实例。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

错误码:

错误码ID 错误信息
202 Not system application.

示例:

let userAuth = new osAccount.UserAuth();

getVersion8+

getVersion(): number

返回版本信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

返回值:

类型 说明
number 返回版本信息。

错误码:

错误码ID 错误信息
202 Not system application.

示例:

let userAuth = new osAccount.UserAuth();
let version: number = userAuth.getVersion();
console.info('getVersion version = ' + version);

getAvailableStatus8+

getAvailableStatus(authType: AuthType, authTrustLevel: AuthTrustLevel): number

获取指定认证类型和认证可信等级的认证能力的可用状态。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
authType AuthType 认证类型。
authTrustLevel AuthTrustLevel 认证的可信等级。

返回值:

类型 说明
number 返回认证能力的可用状态。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid authType or authTrustLevel.
12300117 PIN is expired.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let authType: osAccount.AuthType = osAccount.AuthType.PIN;
let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1;
try {
  let status: number = userAuth.getAvailableStatus(authType, authTrustLevel);
  console.info('getAvailableStatus status = ' + status);
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAvailableStatus exception = code is ${err.code}, message is ${err.message}`);
}

getProperty8+

getProperty(request: GetPropertyRequest, callback: AsyncCallback<ExecutorProperty>): void

基于指定的请求信息获取属性。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
request GetPropertyRequest 请求信息,包括认证类型和属性类型列表。
callback AsyncCallback<ExecutorProperty> 回调函数。如果获取成功,err为null,data为执行器属性信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid request.
12300003 Account not found.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let keys: Array<osAccount.GetPropertyType>  = [
  osAccount.GetPropertyType.AUTH_SUB_TYPE,
  osAccount.GetPropertyType.REMAIN_TIMES,
  osAccount.GetPropertyType.FREEZING_TIME
];
let request: osAccount.GetPropertyRequest = {
  authType: osAccount.AuthType.PIN,
  keys: keys
};
try {
  userAuth.getProperty(request, (err: BusinessError, result: osAccount.ExecutorProperty) => {
    if (err) {
      console.error(`getProperty exception = code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getProperty result = ' + JSON.stringify(result));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getProperty exception = code is ${err.code}, message is ${err.message}`);
}

getProperty8+

getProperty(request: GetPropertyRequest): Promise<ExecutorProperty>

基于指定的请求信息获取属性。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
request GetPropertyRequest 请求信息,包括认证类型和属性类型列表。

返回值:

类型 说明
Promise<ExecutorProperty> Promise对象,返回执行器属性信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid request.
12300003 Account not found.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let keys: Array<osAccount.GetPropertyType> = [
  osAccount.GetPropertyType.AUTH_SUB_TYPE,
  osAccount.GetPropertyType.REMAIN_TIMES,
  osAccount.GetPropertyType.FREEZING_TIME
];
let request: osAccount.GetPropertyRequest = {
  authType: osAccount.AuthType.PIN,
  keys: keys
};
try {
  userAuth.getProperty(request).then((result: osAccount.ExecutorProperty) => {
    console.info('getProperty result = ' + JSON.stringify(result));
  }).catch((err: BusinessError) => {
    console.error(`getProperty error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getProperty exception = code is ${err.code}, message is ${err.message}`);
}

getPropertyByCredentialId14+

getPropertyByCredentialId(credentialId: Uint8Array, keys: Array<GetPropertyType>): Promise<ExecutorProperty>

基于凭据id获取关联执行器的指定属性信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
credentialId Uint8Array 指示凭据索引。
keys Array<GetPropertyType> 指示要查询的属性类型数组。

返回值:

类型 说明
Promise<ExecutorProperty> Promise对象,返回执行器的属性信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid keys.
12300020 Device hardware abnormal.
12300102 The credential does not exist.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let credInfo: osAccount.EnrolledCredInfo[] = [];
async function getProperty() {
  try {
    credInfo = await userIDM.getAuthInfo(osAccount.AuthType.PRIVATE_PIN);
  } catch (e) {
    const err = e as BusinessError;
    console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
    return;
  }
  if (credInfo.length == 0) {
    console.info('no credential infos');
    return;
  }
  let testCredentialId: Uint8Array = credInfo[0].credentialId;
  let keys: Array<osAccount.GetPropertyType> = [
    osAccount.GetPropertyType.AUTH_SUB_TYPE,
    osAccount.GetPropertyType.REMAIN_TIMES,
    osAccount.GetPropertyType.FREEZING_TIME
  ];
  try {
    let userAuth = new osAccount.UserAuth();
    userAuth.getPropertyByCredentialId(testCredentialId, keys).then((result: osAccount.ExecutorProperty) => {
      console.info('getPropertyByCredentialId result = ' + JSON.stringify(result));
    }).catch((err: BusinessError) => {
      console.error(`getPropertyByCredentialId error = code is ${err.code}, message is ${err.message}`);
    });
  } catch (e) {
    const err = e as BusinessError;
    console.error(`getPropertyByCredentialId exception = code is ${err.code}, message is ${err.message}`);
  }
}

setProperty8+

setProperty(request: SetPropertyRequest, callback: AsyncCallback<void>): void

设置可用于初始化算法的属性。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
request SetPropertyRequest 请求信息,包括认证类型和要设置的密钥值。
callback AsyncCallback<void> 回调函数。如果设置成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid request.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let request: osAccount.SetPropertyRequest = {
  authType: osAccount.AuthType.PIN,
  key: osAccount.SetPropertyType.INIT_ALGORITHM,
  setInfo: new Uint8Array([0])
};
try {
  userAuth.setProperty(request, (err: BusinessError) => {
    if (err) {
      console.error(`setProperty failed, error = code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('setProperty successfully');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setProperty exception = code is ${err.code}, message is ${err.message}`);
}

setProperty8+

setProperty(request: SetPropertyRequest): Promise<void>

设置可用于初始化算法的属性。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
request SetPropertyRequest 请求信息,包括身份验证类型和要设置的密钥值。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid request.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let request: osAccount.SetPropertyRequest = {
  authType: osAccount.AuthType.PIN,
  key: osAccount.SetPropertyType.INIT_ALGORITHM,
  setInfo: new Uint8Array([0])
};
try {
  userAuth.setProperty(request).then(() => {
    console.info('setProperty successfully');
  }).catch((err: BusinessError) => {
    console.error(`setProperty failed, error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setProperty exception = code is ${err.code}, message is ${err.message}`);
}

prepareRemoteAuth12+

prepareRemoteAuth(remoteNetworkId: string): Promise<void>

准备远端认证。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
remoteNetworkId string 远端网络Id。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 System service exception.
12300002 Invalid remoteNetworkId.
12300090 Cross-device capability not supported.
12300091 Cross-device communication failed.
12300111 Operation timeout.

示例:

import { distributedDeviceManager } from '@kit.DistributedServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let distributedDeviceMgr = distributedDeviceManager.createDeviceManager("com.example.bundleName");
distributedDeviceMgr.getAvailableDeviceList().then((data: Array<distributedDeviceManager.DeviceBasicInfo>) => {
    try {
      if (data.length > 0 && data[0].networkId != null) {
        userAuth.prepareRemoteAuth(data[0].networkId).then(() => {
          console.info('prepareRemoteAuth successfully');
        }).catch((err: BusinessError) => {
          console.error(`prepareRemoteAuth failed, error = code is ${err.code}, message is ${err.message}`);
        });
      }
    } catch (e) {
      const err = e as BusinessError;
      console.error(`prepareRemoteAuth exception = code is ${err.code}, message is ${err.message}`);
    }
  }
)

auth8+

auth(challenge: Uint8Array, authType: AuthType, authTrustLevel: AuthTrustLevel, callback: IUserAuthCallback): Uint8Array

认证当前用户。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
challenge Uint8Array 指示挑战值,挑战值为一个随机数,用于提升安全性。
authType AuthType 指示认证类型。
authTrustLevel AuthTrustLevel 指示认证结果的信任级别。
callback IUserAuthCallback 回调对象,返回认证结果。

返回值:

类型 说明
Uint8Array 返回取消的上下文ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid challenge, authType or authTrustLevel.
12300013 Network exception.
12300020 Device hardware abnormal.
12300090 Cross-device capability not supported.
12300091 Cross-device communication failed.
12300101 The credential is incorrect.
12300102 The credential does not exist.
12300105 The trust level is not supported.
12300106 The authentication type is not supported.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The authentication service does not exist.
12300114 The authentication service works abnormally.
12300117 PIN is expired.
12300119 Multi-factor authentication failed.
12300120 The credentials are no longer valid.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let challenge: Uint8Array = new Uint8Array([0]);
let authType: osAccount.AuthType = osAccount.AuthType.PIN;
let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1;
try {
  userAuth.auth(challenge, authType, authTrustLevel, {
    onResult: (result: number, extraInfo: osAccount.AuthResult) => {
      console.info('auth result = ' + result);
      console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

auth12+

auth(challenge: Uint8Array, authType: AuthType, authTrustLevel: AuthTrustLevel, options: AuthOptions, callback: IUserAuthCallback): Uint8Array

基于指定的挑战值、认证类型(如口令、人脸、指纹等)、认证可信等级以及可选参数(如账号标识、认证意图等)进行身份认证。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
challenge Uint8Array 指示挑战值,挑战值为一个随机数,用于防止重放攻击,提升安全性。
authType AuthType 指示认证类型。
authTrustLevel AuthTrustLevel 指示认证结果的信任级别。
options AuthOptions 指示认证用户的可选参数集合。
callback IUserAuthCallback 回调对象,返回认证结果。

返回值:

类型 说明
Uint8Array 返回取消的上下文ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid challenge, authType, authTrustLevel or options.
12300003 Account not found.
12300013 Network exception.
12300020 Device hardware abnormal.
12300090 Cross-device capability not supported.
12300091 Cross-device communication failed.
12300101 The credential is incorrect.
12300102 The credential does not exist.
12300105 The trust level is not supported.
12300106 The authentication type is not supported.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The authentication service does not exist.
12300114 The authentication service works abnormally.
12300117 PIN is expired.
12300119 Multi-factor authentication failed.
12300120 The credentials are no longer valid.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let challenge: Uint8Array = new Uint8Array([0]);
let authType: osAccount.AuthType = osAccount.AuthType.PIN;
let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1;
let options: osAccount.AuthOptions = {
  accountId: 100
};
try {
  userAuth.auth(challenge, authType, authTrustLevel, options, {
    onResult: (result: number, extraInfo: osAccount.AuthResult) => {
      console.info('auth result = ' + result);
      console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

authUser8+

authUser(userId: number, challenge: Uint8Array, authType: AuthType, authTrustLevel: AuthTrustLevel, callback: IUserAuthCallback): Uint8Array

认证指定用户。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
userId number 指示用户身份。
challenge Uint8Array 指示挑战值,挑战值为一个随机数,用于提升安全性。
authType AuthType 指示认证类型。
authTrustLevel AuthTrustLevel 指示认证结果的信任级别。
callback IUserAuthCallback 回调对象,返回认证结果。

返回值:

类型 说明
Uint8Array 返回取消的上下文ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid challenge, authType or authTrustLevel.
12300003 Account not found.
12300013 Network exception.
12300020 Device hardware abnormal.
12300090 Cross-device capability not supported.
12300091 Cross-device communication failed.
12300101 The credential is incorrect.
12300102 The credential does not exist.
12300105 The trust level is not supported.
12300106 The authentication type is not supported.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The authentication service does not exist.
12300114 The authentication service works abnormally.
12300117 PIN is expired.
12300119 Multi-factor authentication failed.
12300120 The credentials are no longer valid.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let userID: number = 100;
let challenge: Uint8Array = new Uint8Array([0]);
let authType: osAccount.AuthType = osAccount.AuthType.PIN;
let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1;
try {
  userAuth.authUser(userID, challenge, authType, authTrustLevel, {
    onResult: (result,extraInfo) => {
      console.info('authUser result = ' + result);
      console.info('authUser extraInfo = ' + JSON.stringify(extraInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`authUser exception = code is ${err.code}, message is ${err.message}`);
}

cancelAuth8+

cancelAuth(contextID: Uint8Array): void

取消指定的认证操作。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
contextID Uint8Array 指示身份验证上下文ID,此ID动态生成没有具体值。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid contextId.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userAuth = new osAccount.UserAuth();
let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
let challenge = new Uint8Array([0]);
let contextId: Uint8Array = userAuth.auth(challenge, osAccount.AuthType.PIN, osAccount.AuthTrustLevel.ATL1, {
  onResult: (result: number, extraInfo: osAccount.AuthResult) => {
    console.info('auth result = ' + result);
    console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
  }
});
try {
  userAuth.cancelAuth(contextId);
} catch (e) {
  const err = e as BusinessError;
  console.error(`cancelAuth exception = code is ${err.code}, message is ${err.message}`);
}

PINAuth8+

PIN码认证基类。

系统接口: 此接口为系统接口。

constructor8+

constructor()

创建PIN码认证的实例。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

错误码:

错误码ID 错误信息
202 Not system application.

示例:

let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();

registerInputer8+

registerInputer(inputer: IInputer): void

注册PIN码输入器。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_PIN_AUTH

参数:

参数名 类型 必填 说明
inputer IInputer PIN码输入器,用于获取PIN码。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid inputer.
12300103 The credential inputer already exists.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
let password = new Uint8Array([0, 0, 0, 0, 0]);
try {
  pinAuth.registerInputer({
    onGetData: (authSubType: osAccount.AuthSubType, callback: osAccount.IInputData) => {
      callback.onSetData(authSubType, password);
    }
  });
  console.info('registerInputer success.');
} catch (e) {
  const err = e as BusinessError;
  console.error(`registerInputer exception = code is ${err.code}, message is ${err.message}`);
}

unregisterInputer8+

unregisterInputer(): void

解注册PIN码输入器。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_PIN_AUTH

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.

示例:

let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
pinAuth.unregisterInputer();

InputerManager 9+

凭据输入管理器。

registerInputer9+

static registerInputer(authType: AuthType, inputer: IInputer): void

注册凭据输入器。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL或ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
authType AuthType 认证类型。
inputer IInputer 凭据输入器,用于获取凭据。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid authType or inputer.
12300103 The credential inputer already exists.
12300106 The authentication type is not supported.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let authType: osAccount.AuthType = osAccount.AuthType.DOMAIN;
let password: Uint8Array = new Uint8Array([0, 0, 0, 0, 0]);
try {
  osAccount.InputerManager.registerInputer(authType, {
    onGetData: (authSubType: osAccount.AuthSubType, callback: osAccount.IInputData) => {
      callback.onSetData(authSubType, password);
    }
  });
  console.info('registerInputer success.');
} catch (e) {
  const err = e as BusinessError;
  console.error(`registerInputer exception = code is ${err.code}, message is ${err.message}`);
}

unregisterInputer9+

static unregisterInputer(authType: AuthType): void

解注册凭据输入器。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL或ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
authType AuthType 认证类型。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300002 Invalid authType.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let authType: osAccount.AuthType = osAccount.AuthType.DOMAIN;
try {
  osAccount.InputerManager.unregisterInputer(authType);
  console.info('unregisterInputer success.');
} catch (e) {
  const err = e as BusinessError;
  console.error(`unregisterInputer code is ${err.code}, message is ${err.message}`);
}

DomainPlugin9+

域插件,提供域账号认证功能。

系统接口: 此接口为系统接口。

auth9+

auth(domainAccountInfo: DomainAccountInfo, credential: Uint8Array, callback: IUserAuthCallback): void

认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
credential Uint8Array 指示域账号的凭据。
callback IUserAuthCallback 指示认证结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {
    // mock authentication
    // notify authentication result
    let result: osAccount.AuthResult = {
      token: new Uint8Array([0]),
      remainTimes: 5,
      freezingTime: 0
    };
    callback.onResult(0, result);
  },
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                    callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin);
let userAuth = new osAccount.UserAuth();
let challenge: Uint8Array = new Uint8Array([0]);
let authType: osAccount.AuthType = osAccount.AuthType.DOMAIN;
let authTrustLevel: osAccount.AuthTrustLevel = osAccount.AuthTrustLevel.ATL1;
try {
  userAuth.auth(challenge, authType, authTrustLevel, {
    onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
        console.info('auth resultCode = ' + resultCode);
        console.info('auth authResult = ' + JSON.stringify(authResult));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

authWithPopup10+

authWithPopup(domainAccountInfo: DomainAccountInfo, callback: IUserAuthCallback): void

弹窗认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
callback IUserAuthCallback 指示认证结果回调。

示例:

import { AsyncCallback } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {
    // mock authentication
    // notify authentication result
    let result: osAccount.AuthResult = {
      token: new Uint8Array([0]),
      remainTimes: 5,
      freezingTime: 0
    };
    callback.onResult(0, result);
  },
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

authWithToken10+

authWithToken(domainAccountInfo: DomainAccountInfo, token: Uint8Array, callback: IUserAuthCallback): void

使用授权令牌认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
token Uint8Array 指示PIN码或生物识别认证成功时生成的授权令牌。
callback IUserAuthCallback 指示认证结果回调。

示例:

import { AsyncCallback } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {
    // mock authentication
    // notify authentication result
    let result: osAccount.AuthResult = {
      token: new Uint8Array([0]),
      remainTimes: 5,
      freezingTime: 0
    };
    callback.onResult(0, result);
  },
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

getAccountInfo10+

getAccountInfo(options: GetDomainAccountInfoPluginOptions, callback: AsyncCallback<DomainAccountInfo>): void

查询指定域账号的信息。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
options GetDomainAccountInfoPluginOptions 指示域账号信息。
callback AsyncCallback<DomainAccountInfo> 指示查询结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {
    // mock getting account information
    // notify result
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    let accountInfo: osAccount.DomainAccountInfo = {
      domain: options.domain ? options.domain : "",
      accountName: options.accountName,
      accountId: 'xxxx'
    };
    callback(code, accountInfo);
  },
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

getAuthStatusInfo10+

getAuthStatusInfo(domainAccountInfo: DomainAccountInfo, callback: AsyncCallback<AuthStatusInfo>): void

查询指定域账号的认证状态信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
callback AsyncCallback<AuthStatusInfo> 指示查询结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    let statusInfo: osAccount.AuthStatusInfo = {
      remainTimes: 5,
      freezingTime: 0
    };
    callback(code, statusInfo);
  },
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

bindAccount10+

bindAccount(domainAccountInfo: DomainAccountInfo, localId: number, callback: AsyncCallback<void>): void

绑定指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
localId number 系统账号ID。
callback AsyncCallback<void> 指示绑定结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {
    // mock unbinding operation
    // notify binding result
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    callback(code);
  },
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

unbindAccount10+

unbindAccount(domainAccountInfo: DomainAccountInfo, callback: AsyncCallback<void>): void

解绑指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
callback AsyncCallback<void> 指示绑定结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {
    // mock unbinding operation
    // notify unbinding result
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    callback(code);
  },
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

isAccountTokenValid10+

isAccountTokenValid(domainAccountInfo: DomainAccountInfo, token: Uint8Array, callback: AsyncCallback<boolean>): void

检查指定的域账号令牌是否有效。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
token Uint8Array 指示域账号令牌。
callback AsyncCallback<boolean> 指示检查结果回调。true表示指定的域账号令牌是有效的;false表示指定的域账号令牌是无效的。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {
    // mock checking operation
    // notify checking result
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    callback(code, true);
  },
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
osAccount.DomainAccountManager.registerPlugin(plugin)

getAccessToken10+

getAccessToken(options: GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>): void

根据指定的选项获取域访问令牌。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
options GetDomainAccessTokenOptions 指示获取域访问令牌的选项。
callback AsyncCallback<Uint8Array> 指示结果回调。

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
        callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                  callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                  callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                  callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                        callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {
    // mock getting operation
    // notify result
    let code: BusinessError = {
      code: 0,
      name: "",
      message: ""
    };
    let token: Uint8Array = new Uint8Array([0]);
    callback(code, token);
  }
}
osAccount.DomainAccountManager.registerPlugin(plugin)

DomainAccountManager 9+

域账号管理器类。

registerPlugin9+

static registerPlugin(plugin: DomainPlugin): void

注册域插件。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
plugin DomainPlugin 指示域插件。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300201 The domain plugin has been registered.

示例:

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let plugin: osAccount.DomainPlugin = {
  auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
       callback: osAccount.IUserAuthCallback) => {},
  authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
                callback: osAccount.IUserAuthCallback) => {},
  authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                callback: osAccount.IUserAuthCallback) => {},
  getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
                 callback: AsyncCallback<osAccount.DomainAccountInfo>) => {},
  getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
                      callback: AsyncCallback<osAccount.AuthStatusInfo>) => {},
  bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
                callback: AsyncCallback<void>) => {},
  unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {},
  isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
                      callback: AsyncCallback<boolean>) => {},
  getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {}
}
try {
  osAccount.DomainAccountManager.registerPlugin(plugin);
  console.info('registerPlugin success.');
} catch (e) {
  const err = e as BusinessError;
  console.error(`registerPlugin code is ${err.code}, message is ${err.message}`);
}

unregisterPlugin9+

static unregisterPlugin(): void

注销域插件。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
801 Capability not supported.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

try {
  osAccount.DomainAccountManager.unregisterPlugin();
  console.info('unregisterPlugin success.');
} catch (e) {
  const err = e as BusinessError;
  console.error(`unregisterPlugin code is ${err.code}, message is ${err.message}`);
}

auth10+

auth(domainAccountInfo: DomainAccountInfo, credential: Uint8Array, callback: IUserAuthCallback): void

认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
credential Uint8Array 指示域账号的凭据。
callback IUserAuthCallback 指示认证结果回调。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid domainAccountInfo or credential.
12300003 Domain account does not exist.
12300013 Network exception.
12300101 Authentication failed.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The account authentication service does not exist.
12300114 The account authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
let credential = new Uint8Array([0])
try {
  osAccount.DomainAccountManager.auth(domainAccountInfo, credential, {
    onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
      console.info('auth resultCode = ' + resultCode);
      console.info('auth authResult = ' + JSON.stringify(authResult));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

auth24+

auth(domainAccountInfo: DomainAccountInfo, credential: Uint8Array, options: DomainAccountAuthOptions, callback: IUserAuthCallback): void

认证指定的域账号,支持指定认证选项,如服务器参数。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
credential Uint8Array 指示域账号的凭据。
options DomainAccountAuthOptions 表示域账号认证的选项。
callback IUserAuthCallback 指示认证结果回调。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid domainAccountInfo or credential.
12300003 Domain account does not exist.
12300013 Network exception.
12300101 Authentication failed.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The account authentication service does not exist.
12300114 The account authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
let credential = new Uint8Array([0]);
try {
  let serverParams: Record<string, Object> = {
    "uri": "test.example.com",
    "port": 100
  }
  let authOptions: osAccount.DomainAccountAuthOptions = {
    serverParams: serverParams
  }
  osAccount.DomainAccountManager.auth(domainAccountInfo, credential, authOptions, {
    onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
      console.info('auth resultCode = ' + resultCode);
      console.info('auth authResult = ' + JSON.stringify(authResult));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

authWithPopup10+

authWithPopup(callback: IUserAuthCallback): void

弹框认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限:

API版本11+:N/A

API版本10:ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
callback IUserAuthCallback 指示认证结果回调。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300003 No domain account is bound.
12300013 Network exception.
12300101 Authentication failed.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The account authentication service does not exist.
12300114 The account authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

try {
  osAccount.DomainAccountManager.authWithPopup({
    onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
      console.info('auth resultCode = ' + resultCode);
      console.info('auth authResult = ' + JSON.stringify(authResult));
    }
  })
} catch (e) {
  const err = e as BusinessError;
  console.error(`auth exception = code is ${err.code}, message is ${err.message}`);
}

authWithPopup10+

authWithPopup(localId: number, callback: IUserAuthCallback): void

弹框认证指定的域账号。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限:

API版本11+:N/A

API版本10:ohos.permission.ACCESS_USER_AUTH_INTERNAL

参数:

参数名 类型 必填 说明
localId number 指示绑定域账号的系统账号的本地标识。
callback IUserAuthCallback 指示认证结果回调。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid localId.
12300003 No domain account is bound.
12300013 Network exception.
12300101 Authentication failed.
12300109 The authentication, enrollment, or update operation is canceled.
12300110 The authentication is locked.
12300111 The authentication time out.
12300112 The authentication service is busy.
12300113 The account authentication service does not exist.
12300114 The account authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

try {
  osAccount.DomainAccountManager.authWithPopup(100, {
    onResult: (resultCode: number, authResult: osAccount.AuthResult) => {
      console.info('authWithPopup resultCode = ' + resultCode);
      console.info('authWithPopup authResult = ' + JSON.stringify(authResult));
    }
  })
} catch (e) {
  const err = e as BusinessError;
  console.error(`authWithPopup exception = code is ${err.code}, message is ${err.message}`);
}

hasAccount10+

hasAccount(domainAccountInfo: DomainAccountInfo, callback: AsyncCallback<boolean>): void

检查是否存在指定的域账号。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
callback AsyncCallback<boolean> 指示检查结果回调。true表示指定的域账号已存在;false表示指定的域账号不存在。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid domainAccountInfo.
12300013 Network exception.
12300014 Not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
try {
  osAccount.DomainAccountManager.hasAccount(domainAccountInfo, (err: BusinessError, result: boolean) => {
    if (err) {
      console.error(`call hasAccount failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('hasAccount result: ' + result);
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`hasAccount exception = code is ${err.code}, message is ${err.message}`);
}

hasAccount10+

hasAccount(domainAccountInfo: DomainAccountInfo): Promise<boolean>

检查是否存在指定的域账号。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。

返回值:

类型 说明
Promise<boolean> Promise对象。返回true表示指定的域账号已存在;返回false表示指定的域账号不存在。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid domainAccountInfo.
12300013 Network exception.
12300014 Not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
try {
  osAccount.DomainAccountManager.hasAccount(domainAccountInfo).then((result: boolean) => {
    console.info('hasAccount result: ' + result);
  }).catch((err: BusinessError) => {
      console.error(`call hasAccount failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`hasAccount exception = code is ${err.code}, message is ${err.message}`);
}

updateAccountToken10+

updateAccountToken(domainAccountInfo: DomainAccountInfo, token: Uint8Array, callback: AsyncCallback<void>): void

更新指定域账号的令牌,空令牌表示目标域账号的令牌失效。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
token Uint8Array 指示域账号的令牌。
callback AsyncCallback<void> 回调函数。如果更新成功,err为null,否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid token.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan',
  accountId: '123456'
}
let token = new Uint8Array([0])
try {
  osAccount.DomainAccountManager.updateAccountToken(domainAccountInfo, token, (err: BusinessError) => {
    if (err != null) {
      console.error(`updateAccountToken failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('updateAccountToken successfully');
    }
  })
} catch (e) {
  const err = e as BusinessError;
  console.error(`updateAccountToken exception = code is ${err.code}, message is ${err.message}`);
}

updateAccountToken10+

updateAccountToken(domainAccountInfo: DomainAccountInfo, token: Uint8Array): Promise<void>

更新指定域账号的令牌,空令牌表示目标域账号的令牌失效。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。
token Uint8Array 指示域账号的令牌。

返回值:

类型 说明
Promise<void> Promise对象,无返回结果的Promise对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid token.
12300003 Account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.DomainAccountInfo = {
  domain: 'CHINA',
  accountName: 'zhangsan',
  accountId: '123456'
}
let token = new Uint8Array([0])
try {
  osAccount.DomainAccountManager.updateAccountToken(domainAccountInfo, token).then(() => {
    console.info('updateAccountToken successfully');
  }).catch((err: BusinessError) => {
    console.error(`updateAccountToken failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`updateAccountToken exception = code is ${err.code}, message is ${err.message}`);
}

getAccountInfo10+

getAccountInfo(options: GetDomainAccountInfoOptions, callback: AsyncCallback<DomainAccountInfo>): void

查询指定的域账号信息。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.GET_DOMAIN_ACCOUNTS

参数:

参数名 类型 必填 说明
options GetDomainAccountInfoOptions 指示域账号信息。
callback AsyncCallback<DomainAccountInfo> 指示查询结果回调。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300003 Account not found.
12300013 Network exception.
12300014 Not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.GetDomainAccountInfoOptions = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
try {
  osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo,
    (err: BusinessError, result: osAccount.DomainAccountInfo) => {
    if (err) {
      console.error(`call getAccountInfo failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getAccountInfo result: ' + result);
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAccountInfo exception = code is ${err.code}, message is ${err.message}`);
}

getAccountInfo10+

getAccountInfo(options: GetDomainAccountInfoOptions): Promise<DomainAccountInfo>

查询指定的域账号信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.GET_DOMAIN_ACCOUNTS

参数:

参数名 类型 必填 说明
options GetDomainAccountInfoOptions 指示域账号信息。

返回值:

类型 说明
Promise<DomainAccountInfo> Promise对象,返回指定的域账号信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300003 Account not found.
12300013 Network exception.
12300014 Not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainAccountInfo: osAccount.GetDomainAccountInfoOptions = {
  domain: 'CHINA',
  accountName: 'zhangsan'
}
try {
  osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo)
    .then((result: osAccount.DomainAccountInfo) => {
    console.info('getAccountInfo result: ' + result);
  }).catch((err: BusinessError) => {
    console.error(`call getAccountInfo failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAccountInfo exception = code is ${err.code}, message is ${err.message}`);
}

getAccessToken11+

getAccessToken(businessParams: Record<string, Object>, callback: AsyncCallback<Uint8Array>): void

获取当前域账号的业务访问令牌。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
businessParams Record<string, Object> 指示业务参数,具体格式取决于域插件的实现要求。
callback AsyncCallback<Uint8Array> 指示结果回调。如果获取成功,err返回null,否则为错误对象。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid business parameters.
12300003 Domain account not found.
12300013 Network exception.
12300014 The domain account is not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let businessParams: Record<string, Object> = {
  'clientId': 'xxx',
  'secretId': 'yyy'
};  // depends on the implementation of the domain plugin
try {
  osAccount.DomainAccountManager.getAccessToken(businessParams,
    (err: BusinessError, result: Uint8Array) => {
    if (err) {
      console.error(`getAccessToken failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getAccessToken result: ' + result);
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAccessToken exception = code is ${err.code}, message is ${err.message}`);
}

getAccessToken11+

getAccessToken(businessParams: Record<string, Object>): Promise<Uint8Array>

查询当前域账号的业务访问令牌。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
businessParams Record<string, Object> 指示业务参数,具体格式取决于域插件的实现要求。

返回值:

类型 说明
Promise<Uint8Array> Promise对象,返回业务访问令牌。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300002 Invalid business parameters.
12300003 Domain account not found.
12300013 Network exception.
12300014 The domain account is not authenticated.
12300111 The operation time out.
12300114 The authentication service works abnormally.
12300211 Server unreachable.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let businessParams: Record<string, Object> = {
  'clientId': 'xxx',
  'secretId': 'yyy'
};  // depends on the implementation of the domain plugin
try {
  osAccount.DomainAccountManager.getAccessToken(businessParams)
    .then((result: Uint8Array) => {
    console.info('getAccessToken result: ' + result);
  }).catch((err: BusinessError) => {
    console.error(`getAccessToken failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAccessToken exception = code is ${err.code}, message is ${err.message}`);
}

isAuthenticationExpired12+

isAuthenticationExpired(domainAccountInfo: DomainAccountInfo): Promise<boolean>

判断指定域账号是否登录超期。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS或ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
domainAccountInfo DomainAccountInfo 指示域账号信息。

返回值:

类型 说明
Promise<boolean> Promise对象。返回true表示指定的域账号已登录超期;返回false表示指定的域账号未登录超期。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
801 Capability not supported.
12300001 The system service works abnormally.
12300003 Domain account not found.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let domainInfo: osAccount.DomainAccountInfo =
  {domain: 'testDomain', accountName: 'testAccountName'};
try {
  osAccount.DomainAccountManager.isAuthenticationExpired(domainInfo).then((result: boolean) => {
    console.info('isAuthenticationExpired, result: ' + result);
  }).catch((err: BusinessError) => {
    console.error('isAuthenticationExpired err: ' + err);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error('isAuthenticationExpired exception: ' + e);
}

UserIdentityManager8+

获取用户身份管理类。

系统接口: 此接口为系统接口。

constructor8+

constructor()

用户身份管理类的默认构造函数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

错误码:

错误码ID 错误信息
202 Not system application.

示例:

let userIDM = new osAccount.UserIdentityManager();

openSession8+

openSession(callback: AsyncCallback<Uint8Array>): void

打开会话,获取挑战值。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
callback AsyncCallback<Uint8Array> 回调函数。如果打开会话成功,err为null,data为挑战值;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
try {
  userIDM.openSession((err: BusinessError, challenge: Uint8Array) => {
    if (err) {
      console.error(`openSession exception = code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('openSession challenge = ' + JSON.stringify(challenge));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`openSession exception = code is ${err.code}, message is ${err.message}`);
}

openSession8+

openSession(accountId?: number): Promise<Uint8Array>

打开会话,获取挑战值(用于判断后续的身份认证场景是否处于该会话下,防止重放攻击)。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
accountId12+ number 系统账号标识,默认为空。

返回值:

类型 说明
Promise<Uint8Array> Promise对象,返回挑战值。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.
12300008 Restricted account.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let accountId = 100;
try {
  userIDM.openSession(accountId).then((challenge: Uint8Array) => {
    console.info('openSession challenge = ' + JSON.stringify(challenge));
  }).catch((err: BusinessError) => {
    console.error(`openSession error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`openSession exception = code is ${err.code}, message is ${err.message}`);
}

addCredential8+

addCredential(credentialInfo: CredentialInfo, callback: IIdmCallback): void

添加凭据,添加用户凭据信息,传入凭据添加方法和凭据信息(凭据类型,子类,如果添加用户的非密码凭据,则传入密码身份验证令牌),并获取结果/获取信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
credentialInfo CredentialInfo 指示凭据信息。
callback IIdmCallback 回调对象,返回添加凭据的结果。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid credentialInfo, i.e. authType or authSubType.
12300003 Account not found.
12300008 Restricted account.
12300020 Device hardware abnormal.
12300090 Cross-device capability not supported.
12300091 Cross-device communication failed.
12300101 The token is invalid.
12300106 The authentication type is not supported.
12300109 The authentication, enrollment, or update operation is canceled.
12300111 The operation time out.
12300115 The number of credentials reaches the upper limit.
12300116 Credential complexity verification failed.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let password: Uint8Array = new Uint8Array([0, 0, 0, 0, 0, 0]);
let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
pinAuth.registerInputer({
  onGetData: (authSubType: osAccount.AuthSubType, callback: osAccount.IInputData) => {
    callback.onSetData(authSubType, password);
  }
});
let credentialInfo: osAccount.CredentialInfo = {
  credType: osAccount.AuthType.PIN,
  credSubType: osAccount.AuthSubType.PIN_SIX,
  token: new Uint8Array([]),
  additionalInfo: 'xxx'
};
let userIDM = new osAccount.UserIdentityManager();
userIDM.openSession((err: BusinessError, challenge: Uint8Array) => {
  try {
  userIDM.addCredential(credentialInfo, {
    onResult: (result: number, extraInfo: osAccount.RequestResult) => {
      console.info('addCredential result = ' + result);
      console.info('addCredential extraInfo = ' + extraInfo);
    }
  });
  } catch (e) {
    const err = e as BusinessError;
    console.error(`addCredential exception = code is ${err.code}, message is ${err.message}`);
  }
});

updateCredential8+

updateCredential(credentialInfo: CredentialInfo, callback: IIdmCallback): void

更新凭据。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
credentialInfo CredentialInfo 指示凭据信息。
callback IIdmCallback 回调对象,返回更新凭据的结果。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid credentialInfo, i.e. authType or authSubType.
12300003 Account not found.
12300101 The token is invalid.
12300102 The credential does not exist.
12300106 The authentication type is not supported.
12300109 The authentication, enrollment, or update operation is canceled.
12300111 The operation time out.
12300116 Credential complexity verification failed.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let userAuth: osAccount.UserAuth = new osAccount.UserAuth();
let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
let password: Uint8Array = new Uint8Array([0, 0, 0, 0, 0, 0]);
let credentialInfo: osAccount.CredentialInfo = {
  credType: osAccount.AuthType.PIN,
  credSubType: osAccount.AuthSubType.PIN_SIX,
  token: new Uint8Array([]),
};
pinAuth.registerInputer({
  onGetData: (authSubType: osAccount.AuthSubType, callback: osAccount.IInputData) => {
    callback.onSetData(authSubType, password);
  }
});
userIDM.openSession((err: BusinessError, challenge: Uint8Array) => {
  userAuth.auth(challenge, credentialInfo.credType, osAccount.AuthTrustLevel.ATL1, {
    onResult: (result: number, extraInfo: osAccount.AuthResult) => {
      if (result != osAccount.ResultCode.SUCCESS) {
        return;
      }
      if (extraInfo.token != null) {
        credentialInfo.token = extraInfo.token;
      }
      try {
        userIDM.updateCredential(credentialInfo, {
          onResult: (result: number, extraInfo: osAccount.RequestResult) => {
            console.info('updateCredential result = ' + result);
            console.info('updateCredential extraInfo = ' + extraInfo);
          }
        });
      } catch (e) {
        const err = e as BusinessError;
        console.error(`updateCredential exception = code is ${err.code}, message is ${err.message}`);
      }
    }
  });
});

closeSession8+

closeSession(accountId?: number): void

关闭会话,结束IDM操作。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
accountId12+ number 系统账号标识,默认为空。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: Incorrect parameter types.
12300001 The system service works abnormally.
12300003 Account not found.
12300008 Restricted account.

示例:

let userIDM = new osAccount.UserIdentityManager();
let accountId = 100;
userIDM.closeSession(accountId);

cancel8+

cancel(challenge: Uint8Array): void

根据挑战值取消条目。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
challenge Uint8Array 挑战值。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid challenge.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let challenge: Uint8Array = new Uint8Array([0]);
try {
  userIDM.cancel(challenge);
} catch (e) {
  const err = e as BusinessError;
  console.error(`cancel code is ${err.code}, message is ${err.message}`);
}

delUser8+

delUser(token: Uint8Array, callback: IIdmCallback): void

删除具有身份验证令牌的用户。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
token Uint8Array 身份验证令牌。
callback IIdmCallback 回调对象,返回删除用户的结果。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300101 The token is invalid.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let token: Uint8Array = new Uint8Array([0]);
try {
  userIDM.delUser(token, {
    onResult: (result: number, extraInfo: osAccount.RequestResult) => {
      console.info('delUser result = ' + result);
      console.info('delUser extraInfo = ' + JSON.stringify(extraInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`delUser exception = code is ${err.code}, message is ${err.message}`);
}

delCred8+

delCred(credentialId: Uint8Array, token: Uint8Array, callback: IIdmCallback): void

删除用户凭据信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.MANAGE_USER_IDM

参数:

参数名 类型 必填 说明
credentialId Uint8Array 凭证索引。
token Uint8Array 身份验证令牌。
callback IIdmCallback 回调对象,返回删除凭据的结果。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 The system service works abnormally.
12300002 Invalid credentialId.
12300101 The token is invalid.
12300102 The credential does not exist.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let credentialId: Uint8Array = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
let token: Uint8Array = new Uint8Array([0]);
try {
  userIDM.delCred(credentialId, token, {
    onResult: (result: number, extraInfo: osAccount.RequestResult) => {
        console.info('delCred result = ' + result);
        console.info('delCred extraInfo = ' + JSON.stringify(extraInfo));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`delCred exception = code is ${err.code}, message is ${err.message}`);
}

getAuthInfo8+

getAuthInfo(callback: AsyncCallback<Array<EnrolledCredInfo>>): void

获取认证信息。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
callback AsyncCallback<Array<EnrolledCredInfo>> 回调函数。如果成功,err为null,data为当前用户的所有已注册凭据信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
try {
  userIDM.getAuthInfo((err: BusinessError, result: osAccount.EnrolledCredInfo[]) => {
    if (err) {
      console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getAuthInfo result = ' + JSON.stringify(result));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
}

getAuthInfo8+

getAuthInfo(authType: AuthType, callback: AsyncCallback<Array<EnrolledCredInfo>>): void

获取指定类型的认证信息。使用callback异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
authType AuthType 认证类型。
callback AsyncCallback<Array<EnrolledCredInfo>> 回调函数,如果获取成功,err为null,data为当前用户指定类型的所有已注册凭据信息;否则为错误对象。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid authType.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
try {
  userIDM.getAuthInfo(osAccount.AuthType.PIN,
    (err: BusinessError, result: osAccount.EnrolledCredInfo[]) => {
    if (err) {
      console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('getAuthInfo result = ' + JSON.stringify(result));
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
}

getAuthInfo8+

getAuthInfo(authType: AuthType): Promise<Array<EnrolledCredInfo>>

获取认证信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
authType AuthType 认证类型,表示查询所有认证类型的信息。

返回值:

类型 说明
Promise<Array<EnrolledCredInfo>> Promise对象,返回当前用户指定类型的所有已注册凭据信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid authType.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
try {
  userIDM.getAuthInfo(osAccount.AuthType.PIN).then((result: osAccount.EnrolledCredInfo[]) => {
    console.info('getAuthInfo result = ' + JSON.stringify(result))
  }).catch((err: BusinessError) => {
    console.error(`getAuthInfo error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
}

getAuthInfo12+

getAuthInfo(options?: GetAuthInfoOptions): Promise<Array<EnrolledCredInfo>>

依据提供的可选参数,获取认证信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
options GetAuthInfoOptions 获取认证信息的可选参数集合。默认为空,表示查询当前用户所有已注册凭据信息。

返回值:

类型 说明
Promise<Array<EnrolledCredInfo>> Promise对象,返回当前用户指定类型的所有已注册凭据信息。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid options.
12300003 Account not found.
12300020 Device hardware abnormal.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let options: osAccount.GetAuthInfoOptions = {
  authType: osAccount.AuthType.PIN,
  accountId: 100,
};
try {
  userIDM.getAuthInfo(options).then((result: osAccount.EnrolledCredInfo[]) => {
    console.info('getAuthInfo result = ' + JSON.stringify(result))
  }).catch((err: BusinessError) => {
    console.error(`getAuthInfo error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getAuthInfo exception = code is ${err.code}, message is ${err.message}`);
}

getEnrolledId12+

getEnrolledId(authType: AuthType, accountId?: number): Promise<Uint8Array>

基于凭据类型,以及可选的账号标识,获取已注册的凭据ID。使用Promise异步回调。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
authType AuthType 认证凭据类型
accountId number 系统账号标识,默认为空。

返回值:

类型 说明
Promise<Uint8Array> Promise对象,返回已注册的凭据ID。

错误码:

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 Invalid authType.
12300003 Account not found.
12300020 Device hardware abnormal.
12300102 The credential does not exist.
12300106 The authentication type is not supported.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let userIDM = new osAccount.UserIdentityManager();
let authType: osAccount.AuthType = osAccount.AuthType.PIN;
let accountId = 100;
try {
  userIDM.getEnrolledId(authType, accountId).then((enrolledId: Uint8Array) => {
    console.info('getEnrolledId enrolledId = ' + JSON.stringify(enrolledId));
  }).catch((err: BusinessError) => {
    console.error(`getEnrolledId error = code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getEnrolledId exception = code is ${err.code}, message is ${err.message}`);
}

onCredentialChanged23+

onCredentialChanged(credentialTypes: AuthType[], callback: Callback<CredentialChangeInfo>): void

订阅一种或多种类型的凭据变更事件,通过回调函数获取凭据变更信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
credentialTypes AuthType[] 表示订阅的凭据类型集合。
callback Callback<CredentialChangeInfo> 表示用于接收凭据变更事件的回调函数。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.
12300002 One or more credential types are invalid.
12300106 One or more credential types are not supported.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let identityMgr: osAccount.UserIdentityManager = new osAccount.UserIdentityManager();

const callback: Callback<osAccount.CredentialChangeInfo> = (changeInfo: osAccount.CredentialChangeInfo): void => {
  console.info('credentialType: ' + changeInfo.credentialType
    + ', changeType: ' + changeInfo.changeType
    + ', accountId: ' + changeInfo.accountId
    + ', addedCredentialId: ' + changeInfo.addedCredentialId
    + ', deletedCredentialId: ' + changeInfo.deletedCredentialId
    + ', isSilent: ' + changeInfo.isSilent
  )
}

try {
  identityMgr.onCredentialChanged([osAccount.AuthType.PIN, osAccount.AuthType.FACE], callback);
  console.info('Subscribe to the credential changes successfully');
} catch (e) {
  const err = e as BusinessError;
  console.error(`Failed to subscribe to the credential changes, code is ${err.code}, message is ${err.message}`)
}

offCredentialChanged23+

offCredentialChanged(callback?: Callback<CredentialChangeInfo>): void

取消与指定回调关联的订阅记录,若未指定回调,则取消所有订阅记录。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

需要权限: ohos.permission.USE_USER_IDM

参数:

参数名 类型 必填 说明
callback Callback<CredentialChangeInfo> 表示用于接收凭据变更事件的回调函数。默认为undefined,表示清除所有订阅记录;非undefined时,表示清除与该回调函数关联的订阅记录。

错误码:

以下错误码的详细介绍请参见账号管理错误码通用错误码

错误码ID 错误信息
201 Permission denied.
202 Not system application.
12300001 The system service works abnormally.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let identityMgr: osAccount.UserIdentityManager = new osAccount.UserIdentityManager();

const callback: Callback<osAccount.CredentialChangeInfo> = (changeInfo: osAccount.CredentialChangeInfo): void => {
  console.info('credentialType: ' + changeInfo.credentialType
    + ', changeType: ' + changeInfo.changeType
    + ', accountId: ' + changeInfo.accountId
    + ', addedCredentialId: ' + changeInfo.addedCredentialId
    + ', deletedCredentialId: ' + changeInfo.deletedCredentialId
    + ', isSilent: ' + changeInfo.isSilent
  )
}

try {
  identityMgr.onCredentialChanged([osAccount.AuthType.PIN, osAccount.AuthType.FACE], callback);
  console.info('Subscribe to the credential changes successfully');
} catch (e) {
  const err = e as BusinessError;
  console.error(`Failed to subscribe to the credential changes, code is ${err.code}, message is ${err.message}`)
}

try {
  identityMgr.offCredentialChanged(callback);
  console.info('Unsubscribe from the credential changes successfully');
} catch (e) {
  const err = e as BusinessError;
  console.error(`Failed to unsubscribe from the credential changes, code is ${err.code}, message is ${err.message}`)
}

IInputData8+

密码数据回调。

系统接口: 此接口为系统接口。

onSetData8+

onSetData(authSubType: AuthSubType, data: Uint8Array): void

系统接口: 此接口为系统接口。

通知设置数据。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
authSubType AuthSubType 用于认证的凭据子类型。
data Uint8Array 要设置的数据是凭据,用来在认证、添加、修改凭据操作。

错误码:

错误码ID 错误信息
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300002 Invalid pinSubType.

示例:

let password: Uint8Array = new Uint8Array([0, 0, 0, 0, 0, 0]);
let passwordNumber: Uint8Array = new Uint8Array([1, 2, 3, 4]);
let inputer: osAccount.IInputer = {
  onGetData: (authSubType: osAccount.AuthSubType, callback: osAccount.IInputData) => {
      if (authSubType == osAccount.AuthSubType.PIN_NUMBER) {
        callback.onSetData(authSubType, passwordNumber);
      } else {
        callback.onSetData(authSubType, password);
      }
  }
};

IInputer8+

凭据输入器回调。

系统接口: 此接口为系统接口。

onGetData8+

onGetData: (authSubType: AuthSubType, callback: IInputData, options: GetInputDataOptions) => void

通知调用者获取数据的回调函数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
authSubType AuthSubType 认证凭据子类型。
callback IInputData 指示密码数据回调。
options GetInputDataOptions 回调函数的可选参数集合。

示例:

let password: Uint8Array = new Uint8Array([0, 0, 0, 0, 0, 0]);
let passwordNumber: Uint8Array = new Uint8Array([1, 2, 3, 4]);
let inputer: osAccount.IInputer = {
  onGetData: (authSubType: osAccount.AuthSubType,
    callback: osAccount.IInputData, options: osAccount.GetInputDataOptions) => {
      if (authSubType == osAccount.AuthSubType.PIN_NUMBER) {
        callback.onSetData(authSubType, passwordNumber);
      } else {
        callback.onSetData(authSubType, password);
      }
  }
};
let pinAuth: osAccount.PINAuth = new osAccount.PINAuth();
let result = pinAuth.registerInputer(inputer);
console.info('registerInputer result: ' + result);

IUserAuthCallback8+

表示用户认证回调类。

系统接口: 此接口为系统接口。

onResult8+

onResult: (result: number, extraInfo: AuthResult) => void

身份认证结果回调函数,返回结果码和认证结果信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
result number 表示身份认证结果代码。
extraInfo AuthResult 表示不同情况下的具体信息,如果认证通过,则在extrainfo中返回认证令牌,如果身份验证失败,则在extrainfo中返回剩余的身份验证时间,如果身份验证执行器被锁定,冻结时间将在extrainfo中返回。

示例:

let authCallback: osAccount.IUserAuthCallback = {
  onResult: (result: number, extraInfo: osAccount.AuthResult) => {
    console.info('auth result = ' + result);
    console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
  }
};

onAcquireInfo?8+

onAcquireInfo?: (module: number, acquire: number, extraInfo: Uint8Array) => void

身份认证信息获取回调函数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
module number 指示用于身份验证的执行器类型。
acquire number 指示不同身份验证执行器的tip代码。
extraInfo Uint8Array 保留参数。

示例:

let authCallback: osAccount.IUserAuthCallback = {
  onResult: (result: number, extraInfo: osAccount.AuthResult) => {
    console.info('auth result = ' + result)
    console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
  },
  onAcquireInfo: (module: number, acquire: number, extraInfo: Uint8Array) => {
    console.info('auth module = ' + module);
    console.info('auth acquire = ' + acquire);
    console.info('auth extraInfo = ' + JSON.stringify(extraInfo));
  }
};

IIdmCallback8+

表示身份管理回调类。

系统接口: 此接口为系统接口。

onResult8+

onResult: (result: number, extraInfo: RequestResult) => void

身份管理操作结果回调函数,返回结果码和请求结果信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
result number 表示身份认证结果代码。
extraInfo RequestResult 针对不同情况传递具体信息。

示例:

let idmCallback: osAccount.IIdmCallback = {
  onResult: (result: number, extraInfo: osAccount.RequestResult) => {
    console.info('callback result = ' + result)
    console.info('callback extraInfo = ' + JSON.stringify(extraInfo));
  }
};

onAcquireInfo?8+

onAcquireInfo?: (module: number, acquire: number, extraInfo: Uint8Array) => void

身份管理信息获取回调函数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

参数:

参数名 类型 必填 说明
module number 指示用于身份验证的执行器类型。
acquire number 指示不同身份验证执行器的tip代码。
extraInfo Uint8Array 保留参数。

示例:

let idmCallback: osAccount.IIdmCallback = {
  onResult: (result: number, extraInfo: Object) => {
    console.info('callback result = ' + result)
    console.info('callback onResult = ' + JSON.stringify(extraInfo));
  },
  onAcquireInfo: (module: number, acquire: number, extraInfo: Uint8Array) => {
    console.info('callback module = ' + module);
    console.info('callback acquire = ' + acquire);
    console.info('callback onacquireinfo = ' + JSON.stringify(extraInfo));
  }
};

GetPropertyRequest8+

提供获取属性请求的信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
authType AuthType 身份验证凭据类型。
keys Array<GetPropertyType> 指示要获取的属性类型数组。
accountId12+ number 系统账号标识,默认为undefined。

SetPropertyRequest8+

提供设置属性请求的信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
authType AuthType 身份验证凭据类型。
key SetPropertyType 指示要设置的属性类型。
setInfo Uint8Array 指示要设置的信息。

ExecutorProperty8+

提供执行器的属性。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
result number 指示结果。
authSubType AuthSubType 指示认证凭据子类型。
remainTimes number 指示剩余次数,默认为-1。
freezingTime number 指示冻结时间,单位为ms,默认为-1。
enrollmentProgress10+ string 指示录入进度,默认为空。
sensorInfo10+ string 指示传感器信息,默认为空。
nextPhaseFreezingTime12+ number 指示下次冻结时间,单位为ms,默认为undefined。
credentialLength20+ number 指示凭据长度,默认为undefined。查询生物信息等无定长属性的凭据时返回undefined。

AuthResult8+

表示认证结果的信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
token Uint8Array 指示认证令牌,默认为空。
remainTimes number 指示剩余次数,默认为空。
freezingTime number 指示冻结时间,单位为ms,默认为空。
nextPhaseFreezingTime12+ number 指示下次冻结时间,单位为ms,默认为undefined。
credentialId12+ Uint8Array 指示凭据ID,默认为空。
accountId12+ number 指示系统账号标识,默认为undefined。
pinValidityPeriod12+ number 指示认证有效期,单位为ms,默认为undefined。

CredentialInfo8+

表示凭证信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
credType AuthType 指示凭据类型。
credSubType AuthSubType 指示凭据子类型。
token Uint8Array 指示认证令牌。
accountId12+ number 系统账号标识,默认为undefined。
additionalInfo23+ string 凭据的附加信息,默认为空字符串。

RequestResult8+

表示请求结果的信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
credentialId Uint8Array 指示凭据索引,默认为空。

EnrolledCredInfo8+

表示已注册凭据的信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
credentialId Uint8Array 指示凭据索引。
authType AuthType 指示认证凭据类型。
authSubType AuthSubType 指示认证凭据子类型。
templateId Uint8Array 指示凭据模板ID。
isAbandoned20+ boolean 指示凭据是否废弃。废弃后的凭据可能作为备份凭据保存一段时间。true表示已废弃,false表示未废弃。默认为undefined,表示是否废弃未定义。
validityPeriod20+ number 指示凭据有效期,单位为ms。默认为undefined,表示有效期未定义。

GetPropertyType8+

表示要获取的属性类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
AUTH_SUB_TYPE 1 认证子类型。
REMAIN_TIMES 2 剩余次数。
FREEZING_TIME 3 冻结时间。
ENROLLMENT_PROGRESS10+ 4 录入进度。
SENSOR_INFO10+ 5 传感器信息。
NEXT_PHASE_FREEZING_TIME12+ 6 下次冻结时间。
CREDENTIAL_LENGTH20+ 7 凭据长度。

SetPropertyType8+

表示要设置的属性类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
INIT_ALGORITHM 1 初始化算法。

AuthType8+

表示身份验证的凭据类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
PIN 1 表示PIN认证类型。
FACE 2 表示脸部认证类型。
FINGERPRINT10+ 4 表示指纹认证类型。
RECOVERY_KEY12+ 8 表示键恢复类型。
PRIVATE_PIN14+ 16 表示隐私PIN类型。
COMPANION_DEVICE23+ 64 表示伴随设备认证类型。
DOMAIN9+ 1024 表示域认证类型。

AuthSubType8+

表示用于认证的凭据子类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
PIN_SIX 10000 表示6位凭证。
PIN_NUMBER 10001 表示自定义数字凭证。
PIN_MIXED 10002 表示自定义混合凭据。
PIN_FOUR12+ 10003 表示4位凭证。
PIN_PATTERN12+ 10004 表示图案凭据。
PIN_QUESTION14+ 10005 表示密保问题凭据。
FACE_2D 20000 表示2D 人脸凭证。
FACE_3D 20001 表示3D 人脸凭证。
FINGERPRINT_CAPACITIVE10+ 30000 表示电容式指纹。
FINGERPRINT_OPTICAL10+ 30001 表示光学指纹。
FINGERPRINT_ULTRASONIC10+ 30002 表示超声波指纹。
DOMAIN_MIXED9+ 10240001 表示域认证混合凭证。

AuthTrustLevel8+

表示认证结果的受信任级别的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
ATL1 10000 信任级别 1。
ATL2 20000 信任级别 2。
ATL3 30000 信任级别 3。
ATL4 40000 信任级别 4。

Module8+

表示获取信息的模块的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
FACE_AUTH 1 表示从人脸认证获取的信息。

ResultCode8+

表示身份验证结果码。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
SUCCESS 0 表示身份验证成功或支持此功能。
FAIL 1 表示验证器无法识别用户。
GENERAL_ERROR 2 表示其他错误。
CANCELED 3 表示身份验证已取消。
TIMEOUT 4 表示身份验证已超时。
TYPE_NOT_SUPPORT 5 表示不支持此身份验证类型。
TRUST_LEVEL_NOT_SUPPORT 6 表示不支持身份验证信任级别。
BUSY 7 表示身份验证任务正忙。等待几秒钟,然后重试。
INVALID_PARAMETERS 8 表示参数不正确。
LOCKED 9 指示身份验证器已锁定。
NOT_ENROLLED 10 表示用户尚未注册验证器。

FaceTipsCode8+

表示人脸验证过程中提示的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
FACE_AUTH_TIP_TOO_BRIGHT 1 表示由于高照明,获得的面部图像太亮。
FACE_AUTH_TIP_TOO_DARK 2 表示由于照明度低,获得的面部图像太暗。
FACE_AUTH_TIP_TOO_CLOSE 3 表示面部离设备太近。
FACE_AUTH_TIP_TOO_FAR 4 表示面部离设备太远。
FACE_AUTH_TIP_TOO_HIGH 5 表示设备太高,仅捕捉面部上部。
FACE_AUTH_TIP_TOO_LOW 6 表示设备太低,仅捕捉面部下部。
FACE_AUTH_TIP_TOO_RIGHT 7 表示设备向右偏移,并且仅捕捉面部的右侧部分。
FACE_AUTH_TIP_TOO_LEFT 8 表示设备向左偏移,并且仅捕捉面部的左侧部分。
FACE_AUTH_TIP_TOO_MUCH_MOTION 9 表示面部信息收集过程中面部移动过快。
FACE_AUTH_TIP_POOR_GAZE 10 表示面部未朝向设备。
FACE_AUTH_TIP_NOT_DETECTED 11 表示未检测到人脸。

FingerprintTips8+

表示指纹身份验证过程中提示的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
FINGERPRINT_TIP_GOOD 0 表示采集的图像良好。
FINGERPRINT_TIP_IMAGER_DIRTY 1 表示由于传感器上可疑或检测到污垢,指纹图像噪声过大。
FINGERPRINT_TIP_INSUFFICIENT 2 表示由于检测到的情况,指纹图像噪声太大,无法处理。
FINGERPRINT_TIP_PARTIAL 3 表示仅检测到部分指纹图像。
FINGERPRINT_TIP_TOO_FAST 4 表示指纹图像由于快速运动而不完整。
FINGERPRINT_TIP_TOO_SLOW 5 表示由于缺少运动,指纹图像无法读取。
FINGERPRINT_TIP_FINGER_DOWN10+ 6 表示手指落下。
FINGERPRINT_TIP_FINGER_UP10+ 7 表示手指抬起。

OsAccountInfo

表示系统账号信息。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
shortName12+ string 系统账号的短名称。
系统接口: 此接口为系统接口,默认为空。
isLoggedIn12+ boolean 是否登录。true表示已登录;false表示未登录。
系统接口: 此接口为系统接口,默认为false。

OsAccountType

表示系统账号类型的枚举。

系统能力: SystemCapability.Account.OsAccount。

名称 说明
PRIVATE12+ 1024 隐私账号。隐私账号只能有一个。
系统接口: 此接口为系统接口。

DomainAccountInfo8+

表示域账号信息。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
accountId10+ string 域账号标识。
系统接口: 此接口为系统接口,默认为undefined。
isAuthenticated11+ boolean 指示域账号是否已认证。true表示指定的域账号已认证;false表示指定的域账号未认证。
系统接口: 此接口为系统接口,默认为false。

ConstraintSourceTypeInfo9+

表示约束来源类型信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
localId number 系统账号ID
type ConstraintSourceType 约束来源类型。

ConstraintSourceType9+

表示约束来源类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
CONSTRAINT_NOT_EXIST 0 约束不存在。
CONSTRAINT_TYPE_BASE 1 约束源自系统设置。
CONSTRAINT_TYPE_DEVICE_OWNER 2 约束源自设备所有者设置。
CONSTRAINT_TYPE_PROFILE_OWNER 3 约束源自资料所有者设置。

AuthStatusInfo10+

表示认证状态信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
remainTimes number 剩余次数。
freezingTime number 冻结时间,单位为ms。

GetDomainAccessTokenOptions10+

表示获取域访问令牌的选项。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
domainAccountInfo DomainAccountInfo 域账号的信息。
domainAccountToken Uint8Array 域账号的令牌。
businessParams Record<string, Object> 业务参数,由业务方根据请求协议自定义。
callerUid number 调用方唯一标识符。

DomainAccountAuthOptions24+

表示域账号认证的选项。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
serverParams Record<string, Object> 域账号认证服务器配置参数。默认为undefined。

GetDomainAccountInfoOptions10+

表示查询域账号信息的选项。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
accountName string 域账号名。
domain string 域名。默认为undefined。
serverConfigId12+ string 域账号所属服务器标识。默认为undefined。

GetDomainAccountInfoPluginOptions10+

表示插件查询域账号信息的选项。GetDomainAccountInfoPluginOptions类继承GetDomainAccountInfoOptions

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
callerUid number 调用方唯一标识符。

OsAccountSwitchEventData12+

表示系统账号前后台开始切换和结束切换事件的数据结构。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
fromAccountId number 切换来源系统账号ID。
toAccountId number 切换目标系统账号ID。
displayId23+ number 切换事件发生的逻辑屏ID,默认值为0。

ConstraintChangeInfo23+

表示约束变更信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
constraint string 发生变更的约束
isEnabled boolean 发生变更的约束的使能状态。默认:false。
true表示目标约束已使能;false表示目标约束未使能。

CreateOsAccountOptions12+

表示用于创建系统账号的可选参数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
shortName string 表示账号短名称(用作个人文件夹目录)。
约束:
1. 不允许出现的字符:< > | : " * ? / \
2. 不允许独立出现的字符串:.或..
3. 长度不超过255个字符。
disallowedPreinstalledBundles19+ Array<string> 表示预置应用禁止名单,名单中的应用不可被安装在设备上,默认为空列表。
allowedPreinstalledBundles19+ Array<string> 表示预置应用允许名单,仅名单中的应用可以被安装在设备上,默认为std::nullopt。
token24+ Uint8Array 表示从认证管理接口获取的token,默认为空。

CreateOsAccountForDomainOptions12+

表示用于创建与指定域账号绑定的系统账号的可选参数。继承自CreateOsAccountOptions

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

RemoveOsAccountOptions24+

表示用于删除系统账号的可选参数。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
token Uint8Array 表示从认证管理接口获取的token,默认为空。

GetAuthInfoOptions12+

表示查询认证凭据信息getAuthInfo的可选参数集合。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
authType AuthType 认证类型,默认为undefined。
accountId number 系统账号标识,默认为undefined。

AuthIntent12+

表示认证意图的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
UNLOCK 1 解锁意图。
SILENT_AUTH14+ 2 静默认证意图。
QUESTION_AUTH14+ 3 密保问题认证意图。
ABANDONED_PIN_AUTH20+ 4 废弃PIN码认证意图。用户修改锁屏密码后,旧的PIN码被废弃。废弃PIN存在期间,用户如果忘记密码可以通过废弃PIN认证通过后重置锁屏密码。

RemoteAuthOptions12+

表示远程认证的可选参数集合。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
verifierNetworkId string 凭据验证者的网络标识,默认为空。
collectorNetworkId string 凭据收集者的网络标识,默认为空。
collectorTokenId number 凭据收集者的令牌标识,默认为undefined。

AuthOptions12+

表示认证用户auth的可选参数集合。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
accountId number 系统账号标识,默认为undefined。
authIntent AuthIntent 认证意图,默认为undefined。
remoteAuthOptions RemoteAuthOptions 远程认证选项,默认为undefined。

GetInputDataOptions12+

表示通知调用者获取数据onGetData的可选参数集合。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
challenge Uint8Array 挑战值,默认为undefined。

CredentialChangeInfo23+

表示凭据变更信息。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
changeType CredentialChangeType 表示凭据变更的类型。
isSilent boolean 表示是否为静默变更,静默变更表示变更由系统在后台自动地发起。
credentialType AuthType 表示凭据类型。
accountId number 表示系统账号标识。
addedCredentialId Uint8Array 表示添加的凭据ID,添加凭据和更新凭据操作都会返回该ID。默认为undefined。
deletedCredentialId Uint8Array 表示删除的凭据ID,删除凭据和更新凭据操作都会返回该ID。默认为undefined。

CredentialChangeType23+

表示凭据变更类型的枚举。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 说明
ADD_CREDENTIAL 1 表示添加凭据的变更类型。
UPDATE_CREDENTIAL 2 表示更新凭据的变更类型。
DELETE_CREDENTIAL 3 表示删除凭据的变更类型。

SetOsAccountTypeOptions24+

设置系统账号类型的选项。

系统接口: 此接口为系统接口。

系统能力: SystemCapability.Account.OsAccount

名称 类型 只读 可选 说明
token Uint8Array 表示从认证管理接口获取的token。默认为空。