This article will help to understand Azure B2C user create operation using .net core application.
Below are steps for application setup on Azure Portal
- Go to Azure Portal https://portal.azure.com/
- From All Resources find Azure B2C.
- Go to App Registrations and register the new application.
- Create new client secret that we are going to use to access the Graph API.
- We need to add some permission to application below screen.
Below are the required Nuget packages to install in .net core application
- Microsoft.Graph 4.11.0
- Microsoft.Graph 1.0.0-preview.7
- Microsoft.Graph.Core 2.0.5
Code for the creating the graph client:
1 2 3 4 5 6 7 8 9 |
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(_appSetting.B2CAppId) .WithTenantId(_appSetting.B2CTenantId) .WithClientSecret(_appSetting.B2CClientSecret) .Build(); ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); // Set up the Microsoft Graph service client with client credentials GraphServiceClient graphClient = new GraphServiceClient(authProvider); |
Code to create B2C Users:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var result = graphClient.Users .Request() .AddAsync(new User { GivenName = userDetails.FirstName, Surname = userDetails.LastName, DisplayName = userDetails.FirstName + " " + userDetails.LastName, Identities = new List<ObjectIdentity> { new ObjectIdentity() { SignInType= "emailAddress", Issuer= _appSetting.B2CIssuer, IssuerAssignedId = userDetails.EmailAddress } }, PasswordProfile = new PasswordProfile() { // set ForceChangePasswordNextSignIn so that user need to change the password ForceChangePasswordNextSignIn = true, Password = _appSetting.B2CDefaultPassword }, PasswordPolicies = "DisablePasswordExpiration,DisableStrongPassword" //AdditionalData = extensionInstance }).GetAwaiter().GetResult(); |
Below are the reference links: