44using Microsoft . AspNetCore . Authentication ;
55using Microsoft . AspNetCore . Authentication . Cookies ;
66using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . IdentityModel . Tokens ;
78using SharedLibrary . Cache ;
9+ using System . IdentityModel . Tokens . Jwt ;
810using System . Security . Claims ;
11+ using System . Text ;
912using System . Text . Json ;
1013using UserManagement . Contracts . DTO ;
1114using UserManagement . Sdk . Abstractions ;
@@ -29,7 +32,7 @@ public IActionResult Index()
2932 ViewBag . Error = TempData [ "Error" ] ; // one-time error
3033 return View ( new LoginViewModel ( ) ) ; // empty fields
3134 }
32-
35+
3336 [ HttpPost ]
3437 [ ValidateAntiForgeryToken ]
3538 public async Task < IActionResult > Index ( LoginViewModel model , CancellationToken ct )
@@ -40,7 +43,7 @@ public async Task<IActionResult> Index(LoginViewModel model, CancellationToken c
4043 {
4144 return View ( model ) ;
4245 }
43-
46+
4447 var req = new LoginRequest ( model . Username , model . Password ) ;
4548 var result = await _users . LoginAsync ( req , ct ) ;
4649
@@ -56,7 +59,8 @@ public async Task<IActionResult> Index(LoginViewModel model, CancellationToken c
5659 {
5760 new ( ClaimTypes . NameIdentifier , result . UserId . ToString ( ) ) ,
5861 new ( ClaimTypes . Name , result . UserName ) ,
59-
62+ new ( ClaimTypes . Role , result . role ) ,
63+
6064 } ;
6165
6266 var identity = new ClaimsIdentity (
@@ -74,10 +78,11 @@ await HttpContext.SignInAsync(
7478 } ) ;
7579
7680
77- _cache . SetAccessToken ( result . Token , result . UserId , result . ExpiresAtUtc ) ;
81+ await _cache . SetAccessToken ( result . Token , result . UserId , result . ExpiresAtUtc ) ;
82+ await _cache . SetUserInRoleSet ( result . role , result . UserId , result . ExpiresAtUtc ) ;
7883
7984 var categoriesJson = System . Text . Json . JsonSerializer . Serialize ( result . Categories ) ;
80- _cache . SetUserPermissions ( categoriesJson , result . UserId , result . ExpiresAtUtc ) ;
85+ await _cache . SetUserPermissions ( categoriesJson , result . UserId , result . ExpiresAtUtc ) ;
8186
8287 return RedirectToAction ( "Index" , "Home" , new { area = "Home" } ) ;
8388 }
@@ -138,5 +143,6 @@ public async Task<IActionResult> Logout(CancellationToken ct)
138143 }
139144
140145
146+
141147 }
142148}
0 commit comments