Link User Identities - .NET SDK
On this page
Realm provides many authentication providers to log users into your app. Each provider creates a unique user identity. Realm lets you merge multiple credentials into one user identity.
Example
Consider an application that offers anonymous login. This allows users to explore the app without
registering. If users like the application, they create permanent
accounts. They sign up with SSO or email/password authentication. By default,
this creates a new User
object. The app must link the new
identity with the original User.
You can link identities using the LinkCredentialsAsync(). This links authentication providers to a logged-in User object.
// 1) A user logs on anonymously: var anonUser = await app.LogInAsync(Credentials.Anonymous()); // 2) They create some data, and then decide they want to save // it, which requires creating an Email/Password account. // 3) We prompt the user to log in, and then use that info to // register the new EmailPassword user, and then generate an // EmailPassword credential to link the existing anonymous // account: var email = "caleb@mongodb.com"; var password = "MySekritPwd"; await app.EmailPasswordAuth.RegisterUserAsync( email, password); var officialUser = await anonUser.LinkCredentialsAsync( Credentials.EmailPassword(email, password));
In the example above, we must first register the new email/password user before linking. If you are using any of the other Auth Providers, this step is unnecessary. The following example uses Google authentication instead of EmailPassword:
var anonUser = await app.LogInAsync(Credentials.Anonymous()); var officialUser = await anonUser.LinkCredentialsAsync( Credentials.Google("<google-token>", GoogleCredentialType.AuthCode));