Skip to content

Commit

Permalink
init Email login
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Jan 1, 2024
1 parent b95a86a commit cebd014
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ https://techieshour.wordpress.com/2020/05/11/managing-azure-b2c-users-with-micro

https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider

https://docs.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=csharp

https://docs.microsoft.com/en-us/graph/api/invitation-post?view=graph-rest-1.0&tabs=csharp

https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=csharp#request-1
24 changes: 24 additions & 0 deletions RegisterUsersAzureB2CMsGraph/CreateUser/UserModelB2CEmail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;

namespace RegisterUsersAzureB2CMsGraph.CreateUser;

public class UserModelB2CEmail
{
[Required]
public string DisplayName { get; set; } = string.Empty;

[Required]
public string Email { get; set; } = string.Empty;

[Required]
public string PreferredLanguage { get; set; } = "de";

[Required]
public string Surname { get; set; } = string.Empty;

[Required]
public string GivenName { get; set; } = string.Empty;

[Required]
public DateTimeOffset BirthDate { get; set; } = DateTimeOffset.UtcNow.AddYears(-30);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IActionResult OnGet()
}

[BindProperty]
public UserModelB2CIdentity UserModel { get; set; } = new UserModelB2CIdentity();
public UserModelB2CEmail UserModel { get; set; } = new UserModelB2CEmail();

[BindProperty]
public string? UserPassword { get; set; }
Expand All @@ -40,7 +40,7 @@ public async Task<IActionResult> OnPostAsync()
return Page();
}

var (_, Password, _) = await _msGraphService.CreateFederatedUserWithPasswordAsync(UserModel);
var (_, Password, _) = await _msGraphService.CreateEmailAddressUserWithPasswordAsync(UserModel);

UserPassword = Password;
return OnGet();
Expand Down
12 changes: 6 additions & 6 deletions RegisterUsersAzureB2CMsGraph/Services/MsGraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ await _graphServiceClient.Users
}

// TODO: not working afer Graph SDK 5 update
public async Task<(string Upn, string Password, string Id)> CreateFederatedUserWithPasswordAsync(UserModelB2CIdentity userModel)
public async Task<(string Upn, string Password, string Id)> CreateEmailAddressUserWithPasswordAsync(UserModelB2CEmail userModel)
{
// new user create, email does not matter unless you require to send mails
var password = GetEncodedRandomString();
Expand All @@ -114,16 +114,16 @@ await _graphServiceClient.Users
PreferredLanguage = userModel.PreferredLanguage,
Surname = userModel.Surname,
GivenName = userModel.GivenName,
OtherMails = new List<string> { userModel.Email },
Identities = new List<ObjectIdentity>()
{
OtherMails = [userModel.Email],
Identities =
[
new ObjectIdentity
{
SignInType = "federated",
SignInType = "emailAddress",
Issuer = _aadB2CIssuerDomain,
IssuerAssignedId = userModel.Email
},
},
],
PasswordProfile = new PasswordProfile
{
Password = password,
Expand Down

0 comments on commit cebd014

Please sign in to comment.