Adding cookie authentication

arthur_usercookies
Arthur VALIN 2 years ago
parent 410c7cd4c2
commit 4b31e3ff78

@ -15,4 +15,3 @@
</Router> </Router>
</CascadingBlazoredModal> </CascadingBlazoredModal>

@ -21,11 +21,10 @@ namespace CraftSharp.Controllers
"CurrentUser", user "CurrentUser", user
); );
} }
Console.WriteLine("USER : " + user);
return Ok(new { result = "userCookieSet" }); return Ok(new { result = "userCookieSet" });
} }
[HttpDelete]
public IActionResult DeleteUser() public IActionResult DeleteUser()
{ {
@ -36,10 +35,13 @@ namespace CraftSharp.Controllers
} }
[HttpGet]
public IActionResult GetUser() public IActionResult GetUser()
{ {
var jsonUser = HttpContext.Request.Cookies["CurrentUser"]; var jsonUser = HttpContext.Request.Cookies["CurrentUser"];
return Ok(new { result = JsonConvert.DeserializeObject<CurrentUser>(jsonUser) }); return Ok(jsonUser);
} }
} }
} }

@ -26,15 +26,13 @@ namespace CraftSharp.Pages
private string error { get; set; } private string error { get; set; }
private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); private ConnexionModel loginRequest { get; set; } = new ConnexionModel();
private async Task OnSubmit() private async Task OnSubmit()
{ {
error = null; error = null;
try try
{ {
await AuthStateProvider.Login(loginRequest); await AuthStateProvider.Login(loginRequest);
var stringified = JsonConvert.SerializeObject(loginRequest);
var stringified = JsonConvert.SerializeObject(AuthStateProvider.GetCurrentUser());
var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified); var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified);
NavigationManager.NavigateTo("index"); NavigationManager.NavigateTo("index");

@ -11,7 +11,7 @@
</div> </div>
</Authorized> </Authorized>
</AuthorizeView> </AuthorizeView>
@AuthStateProvider.GetCurrentUser().UserName;
<div class="openingPanel"> <div class="openingPanel">
<img src="/Images/chestBottom.png" class="chest chestOpenBottom @openAnim" /> <img src="/Images/chestBottom.png" class="chest chestOpenBottom @openAnim" />
<img src="/Images/chest.png" class="chest chestOpen @openAnim" /> <img src="/Images/chest.png" class="chest chestOpen @openAnim" />

@ -23,10 +23,7 @@ namespace CraftSharp.Pages
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject] [Inject]
public CustomStateProvider AuthService { get; set; }
[Inject]
public CustomStateProvider AuthStateProvider { get; set; } public CustomStateProvider AuthStateProvider { get; set; }
int NumberOfKeys { get; set; } = 0; int NumberOfKeys { get; set; } = 0;
int CostInKeys { get; set; } = 1; int CostInKeys { get; set; } = 1;
@ -42,7 +39,7 @@ namespace CraftSharp.Pages
items = await DataService.List(0, totalItem); items = await DataService.List(0, totalItem);
NumberOfKeys = AuthService.GetCurrentUser().NumberOfKeys; NumberOfKeys = AuthStateProvider.GetCurrentUser().NumberOfKeys;
} }
bool canOpen() bool canOpen()

@ -1,8 +1,24 @@
@page "/" @page "/"
@namespace CraftSharp.Pages @namespace CraftSharp.Pages
@using CraftSharp.Models;
@using CraftSharp.Services;
@using Microsoft.AspNetCore.Components;
@using Newtonsoft.Json;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@inject CustomStateProvider authService;
@inject HttpClient httpClient;
@{ @{
Layout = "_Layout"; Layout = "_Layout";
Console.WriteLine("==============START==============");
var response = await httpClient.GetAsync($"https://localhost:7139/User/GetUser");
string jsonUser = await response.Content.ReadAsStringAsync();
var user = new ConnexionModel();
if (jsonUser != null && jsonUser.Length != 0)
{
user = JsonConvert.DeserializeObject<ConnexionModel>(jsonUser);
await authService.Login(user);
}
} }
<component type="typeof(App)" render-mode="ServerPrerendered"/> <component type="typeof(App)" render-mode="ServerPrerendered"/>

@ -28,9 +28,9 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddOptions(); builder.Services.AddOptions();
builder.Services.AddAuthorizationCore(); builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<CustomStateProvider>(); builder.Services.AddSingleton<CustomStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<CustomStateProvider>()); builder.Services.AddSingleton<AuthenticationStateProvider>(s => s.GetRequiredService<CustomStateProvider>());
builder.Services.AddScoped<IAuthService, AuthService>(); builder.Services.AddSingleton<IAuthService, AuthService>();
// Add the controller of the app // Add the controller of the app
builder.Services.AddControllers(); builder.Services.AddControllers();

@ -41,10 +41,13 @@ namespace CraftSharp.Services
public void Login(ConnexionModel loginRequest) public void Login(ConnexionModel loginRequest)
{ {
Console.WriteLine("LOGIN : " + loginRequest.UserName);
var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password); var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password);
if (user == null) if (user == null)
{ {
Console.WriteLine("LOGINFAILED");
throw new Exception("User name or password invalid !"); throw new Exception("User name or password invalid !");
} }
} }

@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Components;
using Blazorise; using Blazorise;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System;
namespace CraftSharp.Services namespace CraftSharp.Services
{ {
@ -15,9 +14,6 @@ namespace CraftSharp.Services
{ {
private readonly IAuthService _authService; private readonly IAuthService _authService;
[Inject]
public NavigationManager NavigationManager { get; set; }
private CurrentUser _currentUser { get; set; } private CurrentUser _currentUser { get; set; }
public CustomStateProvider(IAuthService authService) public CustomStateProvider(IAuthService authService)
@ -52,7 +48,6 @@ namespace CraftSharp.Services
CurrentUser user; CurrentUser user;
user = _authService.GetUser(loginParameters.UserName); user = _authService.GetUser(loginParameters.UserName);
_currentUser = user; _currentUser = user;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
@ -60,7 +55,6 @@ namespace CraftSharp.Services
{ {
_currentUser = new CurrentUser(); _currentUser = new CurrentUser();
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
@ -71,18 +65,19 @@ namespace CraftSharp.Services
// No error - Login the user // No error - Login the user
var user = _authService.GetUser(registerParameters.UserName); var user = _authService.GetUser(registerParameters.UserName);
_currentUser = user; _currentUser = user;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
public CurrentUser GetCurrentUser() public CurrentUser GetCurrentUser()
{ {
CurrentUser cacheUser;
if (_currentUser != null && _currentUser.IsAuthenticated) if (_currentUser != null && _currentUser.IsAuthenticated)
{ {
Console.WriteLine("GETUSER: " + _currentUser.UserName);
return _currentUser; return _currentUser;
} }
Console.WriteLine("GETUSER: FAIL");
return new CurrentUser(); return new CurrentUser();
} }
} }

Loading…
Cancel
Save