Link User Identities - .NET SDK¶
On this page
MongoDB Realm provides multiple authentication providers. Each authentication provider maintains its own unique list of user identities that enable users to log into your application. To allow a single user to log into your application in multiple ways, you can associate the user's identities from multiple providers together.
For instance, consider an application that enables users to authenticate via anonymous authentication so they may explore functionality without linking to an existing SSO provider or generating a new password. When that user decides to finally create a full account with an SSO provider or email/password authentication, you need some way of associating the user's original anonymous identity with their new permanent identity.
// 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 = "email@example.com"; var password = "shhhItsASektrit!"; 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));