> ## 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 : pratique exemplaire pour connecter un utilisateur

> Procédure : pratique exemplaire 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 voudrez généralement suivre ce modèle si les menus de votre jeu doivent
« accueillir » l'utilisateur avec un élément qui l'identifie clairement, et que 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 que l'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-CA/build/core-features/common/user/player-identity-xuser)


## Related topics

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