# 用户名/密码

> Provide a username and password option to enable players to authenticate using custom credentials in your game.

###### SDK 最低版本：2.7.2##minimum-sdk-version-272

请查看以下场景，为您游戏中使用用户名/密码的玩家设置身份验证：

* 设置用户名/密码。
* 回归用户登录或创建新用户。
* 将用户从匿名登录更新为通过用户名/密码帐户进行平台登录。

注意：首先需要[初始化](../get-started#install-the-sdk) SDK。

## 设置用户名/密码##set-up-the-username--password-provider

1. 将 Unity Authentication 中的 ID 提供商设置为用户名/密码：
   1. 在 Unity 编辑器菜单中，转到 **Edit（编辑）**> **Project Settings...（项目设置...）**，然后选择 **Services（服务）**> **Authentication（身份验证）**。
   2. 将 **ID Providers（ID 提供商）** 设置为 **Username/Password（用户名/密码）**，然后选择 **Add（添加）**。
   3. 选择 **Save（保存）**。

## 注册或回归用户登录##sign-up-or-sign-in-a-returning-player

使用 `SignUpWithUsernamePasswordAsync` 方法来创建使用用户名/密码凭据的新玩家。

> **Note:**
>
> **注意 1**：**用户名**不区分大小写；最少需要包含 3 个字符，最多可以包含 20 个字符，并且仅字母、数字，以及“*.*”、“*-*”、“*@*”或“*\_*”等符号。**注意 2**：**密码**区分大小写；最少需要包含 8 个字符，最多可以包含 30 个字符，并且至少需要包含 1 个小写字母、1 个大写字母、1 个数字和 1 个符号。

```cs
async Task SignUpWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
        Debug.Log("SignUp is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}
```

使用 `SignInWithUsernamePasswordAsync` 方法来通过用户名/密码凭据登录现有的玩家：

```cs
async Task SignInWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
        Debug.Log("SignIn is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}
```

## 将玩家帐户从匿名更新为用户名/密码##link-an-existing-player-to-username--password

在您设置完匿名身份验证后，如果玩家想从匿名帐户升级，创建用户名/密码帐户并登录，那么请在游戏中提示玩家输入凭据。调用 `AddUsernamePasswordAsync` API 来创建新的用户名/密码帐户并将其添加到玩家名下。

如果 SDK 中存在缓存的玩家，则创建新的用户名/密码并将其添加到缓存的玩家名下。

1. 通过 `SignInAnonymouslyAsync` 登录缓存玩家的帐户。
2. 通过 `AddUsernamePasswordAsync` 创建新帐户并将其添加到缓存玩家的帐户。

> **Note:**
>
> **注意**：添加到帐户后，用户名/密码帐户便无法移除。

```cs
async Task AddUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.AddUsernamePasswordAsync(username, password);
        Debug.Log("Username and password added.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}
```

## 更改玩家的密码##change-a-players-password

向玩家提供用于更改用户名/密码帐户的密码的选项。

用户必须已登录，并且您必须要求用户输入其**当前密码**和**新密码**，然后调用 `UpdatePasswordAsync`。`UpdatePasswordAsync` 会保留用户的身份验证状态，但所有其他已登录设备会要求用户重新登录。

> **Note:**
>
> **注意**：成功调用 `UpdatePasswordAsync` 时，会使用户从所有已缓存用户的设备上退出登录。

```cs
async Task UpdatePasswordAsync(string currentPassword, string newPassword)
{
    try
    {
        await AuthenticationService.Instance.UpdatePasswordAsync(currentPassword, newPassword);
        Debug.Log("Password updated.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}
```
