# ユーザー名/パスワード

> 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

ユーザー名/パスワードを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。

* ユーザー名/パスワードを設定する。
* 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
* プレイヤーを匿名ログインからユーザー名/パスワードのアカウントを介したプラットフォームログインへと更新する。

ノート:最初に SDK を [初期化](../get-started#install-the-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**: **username** (ユーザー名) では、大文字と小文字は区別されません。これは 3 文字以上 20 文字以下である必要があり、文字、数字、記号 (*.*、*-*、*@*、*\_* など) のみがサポートされます。**ノート 2**:**password** (パスワード) では、大文字と小文字が区別されます。これは 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);
    }
}
```
