You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.7 KiB
80 lines
2.7 KiB
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using VeraxShield.composants.formulaires.modeles;
|
|
using VeraxShield.factories;
|
|
using VeraxShield.modele.utilisateurs;
|
|
using VeraxShield.services.UtilisateursDataService;
|
|
|
|
public class DonneurEtat : AuthenticationStateProvider
|
|
{
|
|
private IAuthentificationService _serviceAuthentification;
|
|
public UtilisateurCourant _utilisateurCourant;
|
|
private IUtilisateursDataService _utilisateursDataService;
|
|
|
|
public DonneurEtat(IAuthentificationService service, IUtilisateursDataService utilisateursDataService)
|
|
{
|
|
this._utilisateursDataService = utilisateursDataService;
|
|
this._serviceAuthentification = service;
|
|
}
|
|
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
{
|
|
var identite = new ClaimsIdentity();
|
|
|
|
try
|
|
{
|
|
var userInfo = this.getUtilisateurCourant();
|
|
|
|
if (userInfo.EstAuthentifie)
|
|
{
|
|
var claims = new[] { new Claim(ClaimTypes.Name, this._utilisateurCourant.Pseudo) }.Concat(this._utilisateurCourant.Claims.Select(c => new Claim(c.Key, c.Value)));
|
|
identite = new ClaimsIdentity(claims, "Server authentication");
|
|
}
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
Console.WriteLine("Request failed:" + ex);
|
|
}
|
|
|
|
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identite)));
|
|
}
|
|
|
|
private UtilisateurCourant getUtilisateurCourant()
|
|
{
|
|
if (this._utilisateurCourant != null && this._utilisateurCourant.EstAuthentifie)
|
|
{
|
|
return this._utilisateurCourant;
|
|
}
|
|
|
|
return new UtilisateurCourant();
|
|
}
|
|
|
|
public async Task Connexion(RequeteConnexion requete)
|
|
{
|
|
await this._serviceAuthentification.Connexion(requete);
|
|
|
|
// No error - Login the user
|
|
var user = await this._serviceAuthentification.GetUtilisateur(requete.Pseudo);
|
|
this._utilisateurCourant = user;
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
}
|
|
|
|
public async Task Deconnexion()
|
|
{
|
|
this._utilisateurCourant = null;
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
}
|
|
|
|
public async Task Inscription(RequeteInscription requete)
|
|
{
|
|
await this._serviceAuthentification.Inscription(requete);
|
|
|
|
// No error - Login the user
|
|
// var user = await this._serviceAuthentification.GetUtilisateur(requete.Nom);
|
|
// this._utilisateurCourant = user;
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
}
|
|
|
|
} |