Skip to content

Commit 9da8aca

Browse files
create role set of user ids in cache
1 parent df8592f commit 9da8aca

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ApiIntegrationMvc/Areas/Account/Controllers/LoginController.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
using Microsoft.AspNetCore.Authentication;
55
using Microsoft.AspNetCore.Authentication.Cookies;
66
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.IdentityModel.Tokens;
78
using SharedLibrary.Cache;
9+
using System.IdentityModel.Tokens.Jwt;
810
using System.Security.Claims;
11+
using System.Text;
912
using System.Text.Json;
1013
using UserManagement.Contracts.DTO;
1114
using 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

Comments
 (0)