Skip to content

Commit 46aee68

Browse files
Memory chache to store jwt tiken and home area added in web app.
1 parent 78f115f commit 46aee68

File tree

18 files changed

+194
-50
lines changed

18 files changed

+194
-50
lines changed

ApiIntegrationMvc/ApiIntegrationMvc.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
<ProjectReference Include="..\UserManagement.Sdk\UserManagement.Sdk.csproj" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<Folder Include="Areas\Home\Models\" />
15+
</ItemGroup>
16+
1317
</Project>
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ApiIntegrationMvc.Areas.Account.Models;
22
using Microsoft.AspNetCore.Mvc;
3+
using System.Text.Json;
34
using UserManagement.Contracts.Auth;
45
using UserManagement.Sdk.Abstractions;
56

@@ -9,7 +10,8 @@ namespace ApiIntegrationMvc.Areas.Account.Controllers
910
public class LoginController : Controller
1011
{
1112
private readonly IUserManagementClient _users;
12-
public LoginController(IUserManagementClient users) => _users = users;
13+
private readonly IAccessTokenProvider _cache;
14+
public LoginController(IUserManagementClient users, IAccessTokenProvider cache) => (_users, _cache) = (users, cache);
1315

1416
[HttpGet]
1517
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
@@ -23,22 +25,44 @@ public IActionResult Index()
2325
[ValidateAntiForgeryToken]
2426
public async Task<IActionResult> Index(LoginViewModel model, CancellationToken ct)
2527
{
26-
if (!ModelState.IsValid)
28+
try
2729
{
28-
return View(model);
29-
}
30+
if (!ModelState.IsValid)
31+
{
32+
return View(model);
33+
}
34+
35+
var req = new LoginRequest(model.Username, model.Password);
36+
var result = await _users.LoginAsync(req, ct);
3037

31-
var req = new LoginRequest(model.Username, model.Password);
32-
var result = await _users.LoginAsync(req, ct);
38+
if (result == null || string.IsNullOrWhiteSpace(result.Token))
39+
{
40+
TempData["Error"] = "Invalid username or password.";
41+
return RedirectToAction(nameof(Index)); // ← PRG on failure
42+
}
3343

34-
if (result == null || string.IsNullOrWhiteSpace(result.AccessToken))
44+
45+
_cache.SetAccessToken(result.Token, result.UserId, result.ExpiresAtUtc);
46+
47+
return RedirectToAction("Index", "Home", new { area = "Home" });
48+
}
49+
catch (HttpRequestException hx)
3550
{
36-
TempData["Error"] = "Invalid username or password.";
37-
return RedirectToAction(nameof(Index)); // ← PRG on failure
51+
TempData["Error"] = hx.Message;
52+
return RedirectToAction(nameof(Index)); // ← PRG on failure
53+
}
54+
catch (JsonException jx)
55+
{
56+
TempData["Error"] = jx.Message;
57+
return RedirectToAction(nameof(Index)); // ← PRG on failure
58+
}
59+
catch(Exception ex)
60+
{
61+
TempData["Error"] = "Internal Error. Please contact administrator.";
62+
return RedirectToAction(nameof(Index)); // ← PRG on failure
3863
}
39-
40-
return RedirectToAction("Index", "Home"); // PRG on success too
41-
4264
}
65+
66+
4367
}
4468
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using ApiIntegrationMvc.Areas.Account.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Text.Json;
4+
using UserManagement.Contracts.Auth;
5+
using UserManagement.Sdk.Abstractions;
6+
7+
namespace ApiIntegrationMvc.Areas.Home.Controllers
8+
{
9+
[Area("Home")]
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public IActionResult Index()
20+
{
21+
return View();
22+
}
23+
24+
public IActionResult Privacy()
25+
{
26+
return View();
27+
}
28+
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@using ApiIntegrationMvc
2+
@using ApiIntegrationMvc.Models
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@using ApiIntegrationMvc.Areas.Account.Models
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "~/Views/Shared/_Layout.cshtml"; // or area-specific layout if you want
3+
}

ApiIntegrationMvc/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
// Add MemoryCache globally
99
builder.Services.AddMemoryCache();
10+
builder.Services.AddHttpContextAccessor(); // required for the above
1011

1112
builder.Services.AddUserManagementSdk();
1213

13-
// Register the token provider (memory-based)
14-
builder.Services.AddScoped<IAccessTokenProvider, MemoryCacheAccessTokenProvider>();
1514

1615

1716
// Add services to the container.

ApiIntegrationMvc/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<header>
1616
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
1717
<div class="container-fluid">
18-
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ApiIntegrationMvc</a>
18+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Integration Portal</a>
1919
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
2020
aria-expanded="false" aria-label="Toggle navigation">
2121
<span class="navbar-toggler-icon"></span>

UserManagement.Contracts/Auth/AuthResult.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using System.Threading.Tasks;
66

77
namespace UserManagement.Contracts.Auth
8-
{
9-
public sealed record AuthResult(
10-
string AccessToken,
11-
int ExpiresInSeconds,
12-
string? TokenType = "Bearer");
8+
{
9+
public record AuthResponse(
10+
int UserId,
11+
string UserName,
12+
string Token,
13+
DateTime ExpiresAtUtc
14+
);
1315
}

UserManagement.Sdk/Abstractions/IAccessTokenProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace UserManagement.Sdk.Abstractions
99
public interface IAccessTokenProvider
1010
{
1111
Task<string?> GetAccessTokenAsync(CancellationToken ct = default);
12+
void SetAccessToken(string token, int userId, DateTime expiresAtUtc);
1213
}
1314
}

0 commit comments

Comments
 (0)