feat(ControllerBaseExtensions): Erstellte Erweiterungsmethode zum Login über HttpContext mit Umschlag Empfänger und Rolle.
- Implementiert in HomeController
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using EnvelopeGenerator.Web.Models;
|
||||
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
|
||||
using EnvelopeGenerator.Web.Models;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
@@ -6,6 +9,7 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
{
|
||||
public static class ControllerBaseExtensions
|
||||
{
|
||||
#region Auth
|
||||
public static string? GetClaimValue(this ControllerBase controller, string claimType) => controller.User.FindFirstValue(claimType);
|
||||
|
||||
public static string? GetAuthEnvelopeUuid(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
@@ -23,7 +27,35 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
var env_id_str = controller.User.FindFirstValue(EnvelopeClaimTypes.Id);
|
||||
return int.TryParse(env_id_str, out int env_id) ? env_id : null;
|
||||
}
|
||||
|
||||
public static async Task SignInEnvelopeAsync(this HttpContext context, EnvelopeReceiverDto er, string receiverRole)
|
||||
{
|
||||
var claims = new List<Claim> {
|
||||
new(ClaimTypes.NameIdentifier, er.Envelope!.Uuid),
|
||||
new(ClaimTypes.Hash, er.Receiver!.Signature),
|
||||
new(ClaimTypes.Name, er.Name ?? string.Empty),
|
||||
new(ClaimTypes.Email, er.Receiver.EmailAddress),
|
||||
new(EnvelopeClaimTypes.Title, er.Envelope.Title),
|
||||
new(EnvelopeClaimTypes.Id, er.Envelope.Id.ToString()),
|
||||
new(ClaimTypes.Role, receiverRole)
|
||||
};
|
||||
|
||||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
|
||||
var authProperties = new AuthenticationProperties
|
||||
{
|
||||
AllowRefresh = false,
|
||||
IsPersistent = false
|
||||
};
|
||||
|
||||
await context.SignInAsync(
|
||||
CookieAuthenticationDefaults.AuthenticationScheme,
|
||||
new ClaimsPrincipal(claimsIdentity),
|
||||
authProperties);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region View error
|
||||
//TODO: integrate localizer for ready-to-use views
|
||||
public static ViewResult ViewError(this Controller controller, ErrorViewModel errorViewModel) => controller.View("_Error", errorViewModel);
|
||||
|
||||
@@ -61,5 +93,6 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
Subtitle = "Ein unerwarteter Fehler ist aufgetreten",
|
||||
Body = "Bitte kontaktieren Sie das IT-Team."
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user