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

# Como: prática recomendada para conectar um usuário

> Como: prática recomendada para conectar um usuário

## Exemplo que demonstra como obter silenciosamente o usuário padrão

Um jogo precisa estabelecer um usuário inicial. Este exemplo mostra como fazer isso o mais
cedo possível. Normalmente, você seguiria esse padrão se os menus do seu jogo quisessem
"cumprimentar" o usuário com algo que o identificasse claramente e você não quisesse
exibir a interface do usuário imediatamente.

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

## Exemplo que demonstra como obter um usuário inicial

Assim como no exemplo anterior, o jogo precisa estabelecer um usuário inicial. Neste caso,
o jogo aceita que a interface do usuário seja exibida no momento em que solicita um usuário. A interface do usuário só
é exibida se o sistema não conseguir determinar o usuário padrão.

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

```

## Confira também

[Identidade do usuário e XUser](https://docs.xbox.com/pt-BR/build/core-features/common/user/player-identity-xuser.md)


## Related topics

- [Visão geral da autenticação e do acesso a contas](https://docs.xbox.com/pt-BR/services/xbox-services/fundamentals/identity/auth/live-authentication-overview.md)
- [Práticas recomendadas do Insights](https://docs.xbox.com/pt-BR/services/playfab/data-analytics/legacy/insights/best-practices.md)
- [Usuários](https://docs.xbox.com/pt-BR/build/core-features/common/user/user-toc.md)
- [Práticas recomendadas](https://docs.xbox.com/pt-BR/services/xbox-services/develop/best-practices/index.md)
- [Práticas recomendadas para lidar com o jogo offline](https://docs.xbox.com/pt-BR/services/xbox-services/develop/best-practices/live-best-practices-offline-play.md)
