Class User
This class represents a user in a Atlas App Services application. The credentials are provided by various 3rd party providers (Facebook, Google, etc.). A user can log in to the server and, if access is granted, it is possible to synchronize the local and the remote Realm. Moreover, synchronization is halted when the user is logged out. It is possible to persist a user. By retrieving a user, there is no need to log in to the 3rd party provider again. Persisting a user between sessions, the user's credentials are stored locally on the device, and should be treated as sensitive data.
Implements
Namespace: Realms.Sync
Assembly: Realm.dll
Syntax
public class User : IEquatable<User>
Properties
| Edit this page View SourceAccessToken
Gets this user's access token. This is the user's credential for accessing MongoDB Atlas data and should be treated as sensitive information.
Declaration
public string AccessToken { get; }
Property Value
Type | Description |
---|---|
string | A unique string that can be used to represent this user before the server. |
ApiKeys
Gets a User.ApiKeyClient instance that exposes functionality for managing user API keys.
Declaration
public User.ApiKeyClient ApiKeys { get; }
Property Value
Type | Description |
---|---|
User.ApiKeyClient | A User.ApiKeyClient instance scoped to this User. |
See Also
| Edit this page View SourceApp
Gets the app with which this user is associated.
Declaration
public App App { get; }
Property Value
Type | Description |
---|---|
App | An App instance that owns this user. |
DeviceId
Gets a unique identifier for the device the user logged in to.
Declaration
public string DeviceId { get; }
Property Value
Type | Description |
---|---|
string | A unique string that identifies the current device. |
Functions
Gets a User.FunctionsClient instance that exposes functionality for calling remote Atlas Functions.
Declaration
public User.FunctionsClient Functions { get; }
Property Value
Type | Description |
---|---|
User.FunctionsClient | A User.FunctionsClient instance scoped to this User. |
See Also
| Edit this page View SourceId
Gets the Id of this user in Atlas App Services.
Declaration
public string Id { get; }
Property Value
Type | Description |
---|---|
string | A string that uniquely identifies that user. |
Identities
Gets a collection of all identities associated with this user.
Declaration
public UserIdentity[] Identities { get; }
Property Value
Type | Description |
---|---|
UserIdentity[] | The user's identities across different Credentials.AuthProviders. |
Profile
Gets the profile information for that user.
Declaration
public UserProfile Profile { get; }
Property Value
Type | Description |
---|---|
UserProfile | A UserProfile object, containing information about the user's name, email, and so on. |
Provider
Gets a value indicating which Credentials.AuthProvider this user logged in with.
Declaration
[Obsolete("User.Provider wasn't working consistently and will be removed in a future version. You can get the provider of the user identity instead.")]
public Credentials.AuthProvider Provider { get; }
Property Value
Type | Description |
---|---|
Credentials.AuthProvider | The Credentials.AuthProvider used to login the user. |
RefreshToken
Gets this user's refresh token. This is the user's credential for accessing MongoDB Atlas data and should be treated as sensitive information.
Declaration
public string RefreshToken { get; }
Property Value
Type | Description |
---|---|
string | A unique string that can be used for refreshing the user's credentials. |
State
Gets the current state of the user.
Declaration
public UserState State { get; }
Property Value
Type | Description |
---|---|
UserState | A value indicating whether the user is active, logged out, or an error has occurred. |
Methods
| Edit this page View SourceEquals(User?)
Determines whether this instance and another User instance are equal by comparing their identities.
Declaration
public bool Equals(User? other)
Parameters
Type | Name | Description |
---|---|---|
User | other | The User instance to compare with. |
Returns
Type | Description |
---|---|
bool | true if the two instances are equal; false otherwise. |
GetCustomData()
Gets the custom user data associated with this user in the Realm app.
Declaration
public BsonDocument? GetCustomData()
Returns
Type | Description |
---|---|
BsonDocument | A document containing the user data. |
Remarks
The data is only refreshed when the user's access token is refreshed or when explicitly calling RefreshCustomDataAsync().
See Also
| Edit this page View SourceGetCustomData<T>()
Gets the custom user data associated with this user in the Realm app and parses it to the specified type.
Declaration
public T? GetCustomData<T>() where T : class
Returns
Type | Description |
---|---|
T | A document containing the user data. |
Type Parameters
Name | Description |
---|---|
T | The managed type that matches the shape of the custom data documents. |
Remarks
The data is only refreshed when the user's access token is refreshed or when explicitly calling RefreshCustomDataAsync().
See Also
| Edit this page View SourceGetMongoClient(string)
Gets a MongoClient instance for accessing documents in a MongoDB database.
Declaration
public MongoClient GetMongoClient(string serviceName)
Parameters
Type | Name | Description |
---|---|---|
string | serviceName | The name of the service as configured on the server. |
Returns
Type | Description |
---|---|
MongoClient | A MongoClient instance that can interact with the databases exposed in the remote service. |
LinkCredentialsAsync(Credentials)
Links the current user with a new user identity represented by the given credentials.
Declaration
public Task<User> LinkCredentialsAsync(Credentials credentials)
Parameters
Type | Name | Description |
---|---|---|
Credentials | credentials | The credentials to link with the current user. |
Returns
Type | Description |
---|---|
Task<User> | An awaitable Task<TResult> representing the remote link credentials operation. Upon successful completion, the task result will contain the user to which the credentials were linked. |
Remarks
Linking a user with more credentials, mean the user can login either of these credentials. It also
makes it possible to "upgrade" an anonymous user by linking it with e.g. Email/Password credentials.
Note: It is not possible to link two existing users of Atlas App Services. The provided credentials must not have been used by another user.
Note for email/password auth: To link a user with a new set of EmailPassword(string, string) credentials, you will need to first
register these credentials by calling RegisterUserAsync(string, string).
Examples
The following snippet shows how to associate an email and password with an anonymous user allowing them to login on a different device.
var app = App.Create("app-id")
var user = await app.LogInAsync(Credentials.Anonymous());
// This step is only needed for email password auth - a password record must exist
// before you can link a user to it.
await app.EmailPasswordAuth.RegisterUserAsync("email", "password");
await user.LinkCredentialsAsync(Credentials.EmailPassword("email", "password"));
|
Edit this page
View Source
LogOutAsync()
Removes the user's local credentials and attempts to invalidate their refresh token from the server.
Declaration
public Task LogOutAsync()
Returns
Type | Description |
---|---|
Task | An awaitable Task that represents the remote logout operation. |
RefreshCustomDataAsync()
Re-fetch the user's custom data from the server.
Declaration
public Task<BsonDocument?> RefreshCustomDataAsync()
Returns
Type | Description |
---|---|
Task<BsonDocument> | An awaitable Task<TResult> that represents the remote refresh operation. The result is a MongoDB.Bson.BsonDocument containing the updated custom user data. The value returned by GetCustomData() will also be updated with the new information. |
RefreshCustomDataAsync<T>()
Re-fetch the user's custom data from the server.
Declaration
public Task<T?> RefreshCustomDataAsync<T>() where T : class
Returns
Type | Description |
---|---|
Task<T> | An awaitable Task<TResult> that represents the remote refresh operation. The result is an object containing the updated custom user data. The value returned by GetCustomData<T>() will also be updated with the new information. |
Type Parameters
Name | Description |
---|---|
T | The managed type that matches the shape of the custom data documents. |
ToString()
Returns a string that represents the current object.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A string that represents the current object. |
Overrides
Events
| Edit this page View SourceChanged
Occurs when a property value changes.
Declaration
public event EventHandler? Changed
Event Type
Type | Description |
---|---|
EventHandler |
Operators
| Edit this page View Sourceoperator ==(User?, User?)
Determines whether two User instances are equal.
Declaration
public static bool operator ==(User? user1, User? user2)
Parameters
Type | Name | Description |
---|---|---|
User | user1 | The first user to compare. |
User | user2 | The second user to compare. |
Returns
Type | Description |
---|---|
bool |
|
operator !=(User?, User?)
Determines whether two User instances are different.
Declaration
public static bool operator !=(User? user1, User? user2)
Parameters
Type | Name | Description |
---|---|---|
User | user1 | The first user to compare. |
User | user2 | The second user to compare. |
Returns
Type | Description |
---|---|
bool |
|