Greg Gum
Greg Gum

Reputation: 37885

How to add properties to ApplicationUser in AspNetCore Identity

(This question will be self answered, as I have not found a good answer to it on searching. If there is an existing answer, please let me know.)

How do I add a property to ApplicationUser which is stored in the AspNetUsers table?

Upvotes: 0

Views: 3385

Answers (1)

Greg Gum
Greg Gum

Reputation: 37885

The answer to this question is so easy that it gets missed:

First, add it to the database, then add it to the ApplicationUser class:

public class ApplicationUser : IdentityUser
{
    public int TenantId{ get; set; }
}

This is all you need to do to add TenantId. Now, when you need to know the TenantId of a user, just use the UserManager as usual:

var user = await _userManager.FindByNameAsync(model.Email);

The returned user will have the TenantId populated from the database.

You can save updated custom properties with this:

await _userManager.UpdateAsync(user);

That's it.

Upvotes: 4

Related Questions