1

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);
    }
Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

0

ApplicationUser doesn't have the properties UserName and Email that are being assigned to it in the Register method...

ApplicationUser inherits the UserName and Email properties from the IdentityUser class. Those properties and others are listed here in its documentation.

public class ApplicationUser : IdentityUser
{
}

How do I add claims to the user that has just been created?

One way to add claims to a user is to use the UserManager.AddClaimAsync method. There is an example of doing that here.

In the following snippet, which is similar to your Register method, we add the claim to the new user after checking that result.Succeeded.

var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
    await _userManager.AddClaimAsync(user, new Claim("MyClaimType", "MyClaimValue"));
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467