.NET
Authentication

Authentication

In order to use the abilities of the SDK in your client apps, a SendBird instance should be initiated in each client app through user authentication with SendBird server. The instance communicates and interacts with the server based on authenticated user account, and allows the user to the abilities. The following explains how to authenticate your user with the server.


Initializing with APP_ID

To use the chat features, you should first initialize a SendBird instance using the APP_ID assigned to your SendBird application. Typically, initialization is implemented in the user login screen.

Light Color Skin
Copy
SendBirdClient.Init(APP_ID);

Connect to SendBird server with a user ID

By default, SendBird server can authenticate a user just by unique user ID. Then the server queries the database to check for a match upon the request for connection. If there is no matching user ID found, the server creates a new user account with the user ID. The ID should be unique within a SendBird application to be distinguished from each other, such as a hashed email address or phone number in your service.

This authentification procedure is useful when you are in development or if your service doesn't require additional security.

Note: Explanation on SendBird's usage of Handlers and callbacks can be found under the Event Handler page.

Light Color Skin
Copy
SendBirdClient.Connect(USER_ID, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});

Connect to SendBird server with a user ID and an access token

Using the Platform API, you can create a user along with their own access token, or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token in the SendBirdClient.connect() method which is used for signing in.

  1. Using the user creation API, you can create a SendBird user account with your user's information when the user signs up or in to your service.
  2. Save the user ID along with the issued access token to your persistent storage which is securely managed.
  3. When the user attempts to sign in to the SendBird application, load the user ID and access token from the storage, and then pass them to the SendBirdClient.connect() method.
  4. For security, we recommend updating the user's access token periodically by issuing a new token to replace.

Note: You are able to prevent users without an access token from signing in to the application or restrict their access to read and write messages in your dashboard: Settings > Application > Security > Access Token Policy.

Light Color Skin
Copy
SendBirdClient.Connect(USER_ID, ACCESS_TOKEN, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});

Disconnect from SendBird server

You must disconnect from SendBird server when your user no longer needs to receive messages from an online state. But users would receive push notifications for new messages from their group channels.

When disconnected from the server, all registered handlers and callbacks are removed. That means, it removes all event handlers added through the SendBirdClient.AddChannelHandler() or SendBirdClient.AddConnectionHandler(). It also flushes all internally cached data, such as the cached channels from calling the OpenChannel.GetChannel() or GroupChannel.GetChannel() method.

Note: By default, most of the data related to users, channels, and messages are internally cached in the SendBird instance of a user's client app, which are retrieved by the corresponding query instances or received through the event handlers.

Light Color Skin
Copy
SendBirdClient.Disconnect(() => {
    @Override
    public void onDisconnected() {
        // You are disconnected from SendBird.
    }
});

Update user profile

Using the updateCurrentUserInfo() method, you can update a user's nickname and profile image, as well as their profile picture with a URL.

Light Color Skin
Copy
SendBirdClient.UpdateCurrentUserInfo(NICKNAME, PROFILE_URL, (SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});

Or, you can upload an image directly using the UpdateCurrentUserInfoWithProfileImage() method.

Light Color Skin
Copy
SendBirdClient.UpdateCurrentUserInfoWithProfileImage(NICKNAME, FILE_PATH, (SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});