Docs Menu

Authenticate Users - .NET SDK

On this page

  • Log In
  • Anonymous User
  • Email/Password User
  • API Key User
  • Custom JWT User
  • Custom Function User
  • Facebook User
  • Google User
  • Apple User
  • Log a User Out
  • Retrieve the Current User

MongoDB Realm provides an API for authenticating users using any enabled authentication provider. Instantiate a Credentials object and pass it to the LogInAsync() method to authenticate and obtain a User instance. The Credentials class exposes factory methods that correspond to each of the authentication providers:

Before you can authenticate a user, ensure you have:

If you have enabled Anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information. The following code shows how to do this:

var user = await app.LogInAsync(Credentials.Anonymous());

If you have enabled Email/Password authentication, you can log in using the following code:

var user = await app.LogInAsync(
Credentials.EmailPassword("caleb@example.com", "shhhItsASektrit!"));

If you have enabled API Key authentication, you can log in using the following code:

var user = await app.LogInAsync(Credentials.ApiKey(apiKey));

If you have enabled the Custom JWT authentication provider, you can log in using the following code:

var user =
await app.LogInAsync(Credentials.JWT(jwt_token));

If you have enabled the Custom Function authentication provider, you can log in using the following code:

var functionParameters = new
{
username = "caleb",
password = "shhhItsASektrit!",
IQ = 42,
isCool = false
};
var user =
await app.LogInAsync(Credentials.Function(functionParameters));

The Facebook authentication provider allows you to authenticate users through a Facebook app using their existing Facebook account.

Important
Enable the Facebook Auth Provider

To log a user in with their existing Facebook account, you must configure and enable the Facebook authentication provider for your application.

Important
Do Not Store Facebook Profile Picture URLs

Facebook profile picture URLs include the user's access token to grant permission to the image. To ensure security, do not store a URL that includes a user's access token. Instead, access the URL directly from the user's metadata fields when you need to fetch the image.

var user =
await app.LogInAsync(Credentials.Facebook(facebookToken));

If you have enabled Google authentication, you can log in using the following code:

var user =
await app.LogInAsync(Credentials.Google(googleAuthCode, GoogleCredentialType.AuthCode));

If you have enabled Sign-in with Apple authentication, you can log in using the following code:

var user =
await app.LogInAsync(Credentials.Apple(appleToken));
Tip

If you get a Login failed error saying that the token contains an invalid number of segments, verify that you're passing a UTF-8-encoded string version of the JWT.

Once logged in, you can log out by calling the LogOutAsync() method:

await user.LogOutAsync();
Warning

When a user logs out, you can no longer read or write data in any synced realms that the user opened. As a result, any operation that has not yet completed before the initiating user logs out cannot complete successfully and will likely result in an error. Any data in a write operation that fails in this way will be lost.

Once you have an authenticated user, you can retrieve the User object with the App.CurrentUser property. The CurrentUser object is persisted in local storage, so even if the app shuts down after the initial authentication, you do not need to call LoginAsync again (unless the user logged out). Instead, use Realm.GetInstance(config), where config is a SyncConfiguration object. This approach results in a faster start-up and also enables the user to work offline.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.