Creating UserController

arthur_usercookies
Arthur VALIN 2 years ago
parent 07ce065627
commit 410c7cd4c2

@ -0,0 +1,45 @@
using CraftSharp.Models;
using CraftSharp.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Globalization;
using System.Net;
namespace CraftSharp.Controllers
{
[Microsoft.AspNetCore.Mvc.Route("[controller]/[action]")]
public class UserController : Controller
{
[HttpPost]
public IActionResult SetUser([FromBody] String user)
{
if (user != null)
{
HttpContext.Response.Cookies.Append(
"CurrentUser", user
);
}
Console.WriteLine("USER : " + user);
return Ok(new { result = "userCookieSet" });
}
public IActionResult DeleteUser()
{
this.HttpContext.Response.Cookies.Delete(
"CurrentUser"
);
return Ok(new { result = "userCookieDeleted" });
}
public IActionResult GetUser()
{
var jsonUser = HttpContext.Request.Cookies["CurrentUser"];
return Ok(new { result = JsonConvert.DeserializeObject<CurrentUser>(jsonUser) });
}
}
}

@ -7,6 +7,9 @@ using CraftSharp.Models;
using CraftSharp.Services;
using Blazorise;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Text;
namespace CraftSharp.Pages
{
@ -18,6 +21,8 @@ namespace CraftSharp.Pages
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public HttpClient httpClient { get; set; }
private string error { get; set; }
private ConnexionModel loginRequest { get; set; } = new ConnexionModel();
@ -28,7 +33,11 @@ namespace CraftSharp.Pages
try
{
await AuthStateProvider.Login(loginRequest);
var stringified = JsonConvert.SerializeObject(AuthStateProvider.GetCurrentUser());
var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified);
NavigationManager.NavigateTo("index");
}
catch (Exception ex)
{

@ -26,8 +26,6 @@ namespace CraftSharp.Pages
public CustomStateProvider AuthService { get; set; }
[Inject]
public CustomStateProvider AuthStateProvider { get; set; }
[CascadingParameter]
public Task<AuthenticationState> Context { get; set; }
int NumberOfKeys { get; set; } = 0;
int CostInKeys { get; set; } = 1;

@ -18,6 +18,7 @@ using CraftSharp;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System;
using Microsoft.JSInterop;
using CraftSharp.Controllers;
var builder = WebApplication.CreateBuilder(args);
@ -36,7 +37,7 @@ builder.Services.AddControllers();
// Add the localization to the app and specify the resources path
builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
builder.Services.AddScoped<UserController>();
builder.Services.AddHttpClient();
builder.Services.AddBlazoredModal();

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

Loading…
Cancel
Save