How To Add Claims In Asp.net Core Identity After Login
Adding extra claims in ASP.NET Cadre web applications
The ASP.Net Core documentation explains thoroughly what a claim is and how to check for a claim. However, there is no information well-nigh how to create a new claim and adhere information technology to the user object.
Here we are going to close this gap and provide you with step-past-stride instruction on how to add a new claim and utilise it on views (pages) or in any other part of your ASP.NET Core application.
Problem
Allow's describe the task nosotros are going to solve.
- We have an ASP.NET Core (two.1+) project with the default authentication/authorization functionality provided by the ASP.NET Identity library
- We have a user's name field (e.g.
ContactName
) in our identity model grade (permit it beApplicationUser
). - We want to add the value of that
ContactName
column to user's claims and and so show it on our home folio (instead of user'southward email shown past default) when the user is logged in.
Here are the steps nosotros are going to perform to reach our goal.
Step 1: Calculation a new claim
1.ane. Create a custom "claims principle" factory
We demand an implementation of IUserClaimsPrincipalFactory
which volition add necessary data (ContactName
in our case) to the user'due south claims. The simplest way to do information technology is to derive our new class from the default implementation of IUserClaimsPrincipalFactory
and to override one method — GenerateClaimsAsync
:
public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public MyUserClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
} protected override async Job<ClaimsIdentity>GenerateClaimsAsync(ApplicationUser user)
{
var identity = look base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ContactName", user.ContactName ?? "[Click to edit profile]"));
render identity;
}
}
ane.two Annals new form in the DI container
Then we need to annals our new class in the dependency injection container. The best manner for that is to use theAddClaimsPrincipalFactory
extension method:
public void ConfigureServices(IServiceCollection services)
{
. . . . .
services.AddDefaultIdentity<ApplicationUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>(); //<---- add this line
}
That's it. The new merits with the ID ContactName
will be added to the object which represents the current user.
Now we need to go the value of the new claim on our Razor view (folio) or anywhere else in your application.
Step ii: Getting the new merits
To get the value of whatever claim, we but need a reference to the ClaimsPrincipal
object which represents the currently logged user. This object tin can be accessed via User
property of whatever Razor view (page) or past HttpContext.User
in the controller classes or, in general, in whatever place where you have admission to a HttpContext
object.
ClaimsPrincipal
contains the list of all claims associated with the current user and yous can phone call its FindFirst
method to become the necessary claim and then read the Value
property of that claim.
And so, we just need to open _LoginPartical.cshtml
file in Pages/Shared/
(or Views/Shared/
) binder and replace the following line:
<a asp-area="" asp-controller="Manage" asp-action="Index" championship="Manage">Hello @User.Identity.Proper noun!</a>
with this ane:
<a asp-expanse="" asp-controller="Manage" asp-activeness="Index" title="Manage">Hello @(User.FindFirst("ContactName").Value)!</a>
Done!
Now instead of something like How-do-you-do john.doe@yourcompany.com
at the top of your web-page you will see something like this:
Originally published at .NET stories weblog on May 14, 2019.
How To Add Claims In Asp.net Core Identity After Login,
Source: https://levelup.gitconnected.com/add-extra-user-claims-in-asp-net-core-web-applications-1f28c98c9ec6
Posted by: barrowsfincureplarl.blogspot.com
0 Response to "How To Add Claims In Asp.net Core Identity After Login"
Post a Comment