> ## 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.

# Procédure : bonne pratique pour connecter un utilisateur

> Procédure : bonne pratique pour connecter un utilisateur

## Exemple montrant comment obtenir silencieusement l'utilisateur par défaut

Un jeu doit établir un utilisateur initial. Cet exemple montre comment y parvenir le plus tôt
possible. Vous suivrez généralement ce modèle si les menus de votre jeu doivent
« accueillir » l'utilisateur avec un élément l'identifiant clairement, et si vous ne voulez pas
afficher immédiatement l'interface utilisateur.

```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;
}
```

## Exemple montrant comment obtenir un utilisateur initial

Comme dans l'exemple précédent, le jeu doit établir un utilisateur initial. Dans ce cas,
le jeu accepte qu'une interface utilisateur s'affiche au moment où il demande un utilisateur. L'interface utilisateur
ne s'affiche que si le système ne peut pas déterminer l'utilisateur par défaut.

```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;
}

```

## Voir aussi

[Identité de l'utilisateur et XUser](/fr/build/core-features/common/user/player-identity-xuser)


## Related topics

- [XR-115 Ajout et retrait d'utilisateurs ou de manettes pendant le jeu](/fr/publishing/certification/xr/xr-115.md)
- [XBOX Requirements pour les jeux XBOX](/fr/publishing/certification/xbox-requirements.md)
- [Exigences XBOX testées lors de la certification pour les jeux sur console XBOX](/fr/publishing/certification/console-cert-tested-requirements.md)
- [XR-037 Dépendances vis-à-vis des packages de contenu](/fr/publishing/certification/xr/xr-037.md)
- [XR-047 Accès au profil utilisateur](/fr/publishing/certification/xr/xr-047.md)
