I created a project based on the Microsoft authorization example which generated a class named ApplicationUser. I am trying to add claims to a user on account creation.
Based on this SO post I would add the claims to the constructor that creates the user. However that constructor doesnt seem to explicitly exist in the ApplicationUser class (but login works fine). How do I add claims to the user that has just been created?
Further, the class ApplicationUser doesnt have the properties UserName and Email that are being assigned to it in the Register method, which makes me think there is a lot going on in the background I'm missing.
ApplicationUser.cs
namespace xxx.Models
{
public class ApplicationUser : IdentityUser
{
}
}
AccountController.cs
private readonly UserManager<ApplicationUser> _userManager;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
}
.........
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation("User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}