Skip to content

Commit be654dc

Browse files
committed
Created custom claims, adding email and full name to access
1 parent 55a50b1 commit be654dc

File tree

7 files changed

+76
-8
lines changed

7 files changed

+76
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace LeaveManagement.Common.Constants
8+
{
9+
public static class UserClaims
10+
{
11+
public const string Email = "email";
12+
public const string FullName = "fullName";
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-ef": {
6+
"version": "7.0.7",
7+
"commands": [
8+
"dotnet-ef"
9+
]
10+
}
11+
}
12+
}

LeaveManagement.Web/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Identity.UI.Services;
88
using Microsoft.EntityFrameworkCore;
99
using Serilog;
10+
using LeaveManagement.Web.Services.Identity;
1011

1112
var builder = WebApplication.CreateBuilder(args);
1213

@@ -18,7 +19,8 @@
1819

1920
builder.Services.AddDefaultIdentity<Employee>(options => options.SignIn.RequireConfirmedAccount = true)
2021
.AddRoles<IdentityRole>()
21-
.AddEntityFrameworkStores<ApplicationDbContext>();
22+
.AddEntityFrameworkStores<ApplicationDbContext>()
23+
.AddClaimsPrincipalFactory<CustomClaimService>();
2224

2325
builder.Services.AddHttpContextAccessor();
2426

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using LeaveManagement.Common.Constants;
2+
using LeaveManagement.Data;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.Extensions.Options;
5+
using System.Security.Claims;
6+
7+
namespace LeaveManagement.Web.Services.Identity
8+
{
9+
public class CustomClaimService : UserClaimsPrincipalFactory<Employee>
10+
{
11+
12+
public CustomClaimService(
13+
UserManager<Employee> userManager,
14+
IOptions<IdentityOptions> optionsAccessor) : base(userManager, optionsAccessor)
15+
{
16+
}
17+
18+
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(Employee user)
19+
{
20+
var identity = await base.GenerateClaimsAsync(user);
21+
identity.AddClaim(new Claim(UserClaims.FullName, $"{user.FirstName} {user.LastName}"));
22+
identity.AddClaim(new Claim(UserClaims.Email, user.Email));
23+
foreach (var role in await UserManager.GetRolesAsync(user))
24+
{
25+
identity.AddClaim(new Claim(ClaimTypes.Role, role));
26+
}
27+
return identity;
28+
}
29+
}
30+
31+
}

LeaveManagement.Web/Views/Shared/_AdminLayout.cshtml

+7-7
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@
8080
<li>
8181
@if (SignInManager.IsSignedIn(User))
8282
{
83-
var user = await UserManager.GetUserAsync(User);
8483
<div class="user-profile d-flex no-block dropdown m-t-20">
8584
<div class="user-pic">
8685
<img src="~/images/users/1.jpg" alt="users"
8786
class="rounded-circle" width="40" />
8887
</div>
8988
<div class="user-content hide-menu m-l-10">
90-
<h5 class="m-b-0 user-name font-medium">
91-
@user.FirstName @user.LastName
92-
</h5>
93-
<span style="display: block" class="op-5 user-email">
94-
@user.Email</span>
95-
<span style="display: block" class="op-5 user-email">@User.FindFirstValue(ClaimTypes.Role)</span>
89+
<h5 class="m-b-0 user-name font-medium">
90+
@User.FindFirstValue(UserClaims.FullName)
91+
</h5>
92+
<span style="display: block" class="op-5 user-email">
93+
@User.FindFirstValue(UserClaims.Email)
94+
</span>
95+
<span style="display: block" class="op-5 user-email">@User.FindFirstValue(ClaimTypes.Role)</span>
9696
</div>
9797
</div>
9898
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=LeaveManagementNet7;Trusted_Connection=True;MultipleActiveResultSets=true"
4+
}
25
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=LeaveManagementNet7;Trusted_Connection=True;MultipleActiveResultSets=true;Encrypt=False"
4+
//"DefaultConnection": "Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"
5+
}
6+
}

0 commit comments

Comments
 (0)