> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to: ユーザーをサインインさせるためのベスト プラクティス

> How to: ユーザーをサインインさせるためのベスト プラクティス

## 既定のユーザーをサイレントに取得する方法を示す例

ゲームは、最初のユーザーを確立する必要があります。この例では、これをできるだけ早く達成する方法を示します。通常、ゲームのメニューでユーザーを明確に識別する何かで「あいさつ」したいが、UI をすぐに表示したくない場合には、このパターンに従うことが望ましいでしょう。

```c++ theme={null}
HRESULT Identity_TrySignInDefaultUserSilently(_In_ XTaskQueueHandle asyncQueue)
{
    XAsyncBlock* asyncBlock = new XAsyncBlock();
    asyncBlock->queue = asyncQueue;
    asyncBlock->callback = Identity_TrySignInDefaultUserSilently_Callback;

    // Request to silently sign in the default user.
    HRESULT hr = XUserAddAsync(XUserAddOptions::AddDefaultUserSilently, asyncBlock);

    if (FAILED(hr))
    {
        delete asyncBlock;
    }

    return hr;
}

void CALLBACK Identity_TrySignInDefaultUserSilently_Callback(_In_ XAsyncBlock* asyncBlock)
{
    // NOTE: XUserAddResult will add a Ref to the passed-in XUserHandle.
    XUserHandle newUser = nullptr;
    HRESULT hr = XUserAddResult(asyncBlock, &newUser);

    if (SUCCEEDED(hr))
    {
        // TO DO: create Xbox services context and do something with that user.
        // ...
    }
    else 
    {
        // You are here because there's no default user or because of an error.
        // Display some UI, such as "Press A or Enter to continue," and try the sign-in with the UI flow.
    }

    delete asyncBlock;
}
```

## 最初のユーザーを取得する方法を示す例

前の例と同様に、ゲームは最初のユーザーを確立する必要があります。この場合、ゲームはユーザーを要求した時点で UI が表示されることを許容します。UI はシステムが既定のユーザーを判別できない場合にのみ表示されます。

```c++ theme={null}
HRESULT Identity_TrySignInDefaultUser(_In_ XTaskQueueHandle asyncQueue)
{
    XAsyncBlock* asyncBlock = new XAsyncBlock();
    asyncBlock->queue = asyncQueue;
    asyncBlock->callback = Identity_TrySignInDefaultUser_Callback;

    // Request to silently sign in the default user.
    HRESULT hr = XUserAddAsync(XUserAddOptions::AddDefaultUserAllowingUI, asyncBlock);

    if (FAILED(hr))
    {
        delete asyncBlock;
    }

    return hr;
}

void CALLBACK Identity_TrySignInDefaultUser_Callback(_In_ XAsyncBlock* asyncBlock)
{
    // NOTE: XUserAddResult will add a Ref to the passed-in XUserHandle.
    XUserHandle newUser = nullptr;
    HRESULT hr = XUserAddResult(asyncBlock, &newUser);

    if (SUCCEEDED(hr))
    {
        // TO DO: create Xbox services context and do something with that user.
        // ...
    }
    else
    {
        // You are probably here because someone canceled out of the UI flow to 
        // sign in or an expected error occurred.
        // Display some UI, such as "Press A or Enter to continue," and try the sign-in 
        // again with the UI flow.
    }

    delete asyncBlock;
}

```

## 関連項目

[ユーザー ID と XUser](/build/core-features/common/user/player-identity-xuser)


## Related topics

- [ユーザー ID と XUser](/ja-jp/build/core-features/common/user/player-identity-xuser.md)
- [ユーザー](/ja-jp/build/core-features/common/user/user-toc.md)
- [XBOX services 呼び出しのベストプラクティス](/ja-jp/services/xbox-services/develop/best-practices/live-best-practices-calling-xbl.md)
- [ログインの基本とベスト プラクティス](/ja-jp/services/playfab/identity/player-identity/login/login-basics-best-practices.md)
- [XR-115 プレイ中のユーザーまたはコントローラーの追加と削除](/ja-jp/publishing/certification/xr/xr-115.md)
