Adding cookie authentication

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

@ -2,17 +2,16 @@
<CascadingBlazoredModal>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(CraftLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(CraftLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<CascadingAuthenticationState>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(CraftLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</CascadingAuthenticationState>
</NotFound>
</Router>
</CascadingBlazoredModal>

@ -21,11 +21,10 @@ namespace CraftSharp.Controllers
"CurrentUser", user
);
}
Console.WriteLine("USER : " + user);
return Ok(new { result = "userCookieSet" });
}
[HttpDelete]
public IActionResult DeleteUser()
{
@ -36,10 +35,13 @@ namespace CraftSharp.Controllers
}
[HttpGet]
public IActionResult GetUser()
{
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 ConnexionModel loginRequest { get; set; } = new ConnexionModel();
private async Task OnSubmit()
{
error = null;
try
{
await AuthStateProvider.Login(loginRequest);
var stringified = JsonConvert.SerializeObject(AuthStateProvider.GetCurrentUser());
var stringified = JsonConvert.SerializeObject(loginRequest);
var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified);
NavigationManager.NavigateTo("index");

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

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

@ -1,8 +1,24 @@
@page "/"
@namespace CraftSharp.Pages
@using CraftSharp.Models;
@using CraftSharp.Services;
@using Microsoft.AspNetCore.Components;
@using Newtonsoft.Json;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@inject CustomStateProvider authService;
@inject HttpClient httpClient;
@{
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"/>

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

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

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

Loading…
Cancel
Save