Skip to content

Commit 78f115f

Browse files
Login by calling user management (um) api. um sdk added in web app. serilog added in web app. um api url added in app settings. memory cache added. um contracts updated. AccesstokenProvider code, um options code, polices and service collection code added. Um Client added
1 parent 3087f10 commit 78f115f

21 files changed

+294
-42
lines changed

ApiIntegrationMvc/ApiIntegrationMvc.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\UserManagement.Sdk\UserManagement.Sdk.csproj" />
11+
</ItemGroup>
12+
913
</Project>

ApiIntegrationMvc/Areas/Account/Controllers/LoginController.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using ApiIntegrationMvc.Areas.Account.Models;
22
using Microsoft.AspNetCore.Mvc;
3+
using UserManagement.Contracts.Auth;
4+
using UserManagement.Sdk.Abstractions;
35

46
namespace ApiIntegrationMvc.Areas.Account.Controllers
57
{
68
[Area("Account")]
79
public class LoginController : Controller
810
{
11+
private readonly IUserManagementClient _users;
12+
public LoginController(IUserManagementClient users) => _users = users;
13+
914
[HttpGet]
1015
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
1116
public IActionResult Index()
@@ -16,18 +21,23 @@ public IActionResult Index()
1621

1722
[HttpPost]
1823
[ValidateAntiForgeryToken]
19-
public async Task<IActionResult> Index(LoginViewModel model)
24+
public async Task<IActionResult> Index(LoginViewModel model, CancellationToken ct)
2025
{
2126
if (!ModelState.IsValid)
2227
{
2328
return View(model);
2429
}
25-
26-
if(model.Username == "admin" && model.Password == "123")
27-
return RedirectToAction("Index", "Home"); // PRG on success too
30+
31+
var req = new LoginRequest(model.Username, model.Password);
32+
var result = await _users.LoginAsync(req, ct);
33+
34+
if (result == null || string.IsNullOrWhiteSpace(result.AccessToken))
35+
{
36+
TempData["Error"] = "Invalid username or password.";
37+
return RedirectToAction(nameof(Index)); // ← PRG on failure
38+
}
2839

29-
TempData["Error"] = "Invalid username or password.";
30-
return RedirectToAction(nameof(Index)); // ← PRG on failure
40+
return RedirectToAction("Index", "Home"); // PRG on success too
3141

3242
}
3343
}

ApiIntegrationMvc/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
using UserManagement.Sdk;
2+
using UserManagement.Sdk.Abstractions;
3+
using UserManagement.Sdk.Extensions;
4+
15
var builder = WebApplication.CreateBuilder(args);
26

7+
8+
// Add MemoryCache globally
9+
builder.Services.AddMemoryCache();
10+
11+
builder.Services.AddUserManagementSdk();
12+
13+
// Register the token provider (memory-based)
14+
builder.Services.AddScoped<IAccessTokenProvider, MemoryCacheAccessTokenProvider>();
15+
16+
317
// Add services to the container.
418
builder.Services.AddControllersWithViews();
519

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
1+
{
2+
"UserManagement": {
3+
"BaseAddress": "https://localhost:7093/",
4+
"Timeout": "00:00:30",
5+
"EnableResiliencePolicies": true
76
}
87
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"UserManagement": {
3+
"BaseAddress": "http://localhost:7093/",
4+
"Timeout": "00:00:30",
5+
"EnableResiliencePolicies": true
6+
}
7+
}

ApiIntegrationMvc/appsettings.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
2+
"Serilog": {
3+
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Formatting.Compact", "Serilog.Filters.Expressions" ],
4+
"MinimumLevel": {
45
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
7-
},
8-
"AllowedHosts": "*"
6+
"Override": {
7+
"Microsoft": "Warning",
8+
"System": "Warning"
9+
}
10+
},
11+
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithProcessId" ],
12+
"WriteTo": [
13+
{
14+
"Name": "File",
15+
"Args": {
16+
"path": "logs/dev-app-.clef",
17+
"rollingInterval": "Day",
18+
"rollOnFileSizeLimit": true,
19+
"retainedFileCountLimit": 7,
20+
"shared": true,
21+
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
22+
},
23+
"restrictedToMinimumLevel": "Information"
24+
}
25+
]
26+
}
927
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace UserManagement.Contracts.Auth
8+
{
9+
public sealed record AuthResult(
10+
string AccessToken,
11+
int ExpiresInSeconds,
12+
string? TokenType = "Bearer");
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace UserManagement.Contracts.Auth
8+
{
9+
public sealed record LoginRequest(string Username, string Password);
10+
}

UserManagement.Contracts/Class1.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace UserManagement.Contracts.Errors
8+
{
9+
internal class ApiProblemDetails
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)