laatzen/LaaProductionWeb/LaaProduction.Web/Controllers/HomeController.cs
2023-08-08 11:11:16 +02:00

81 lines
2.3 KiB
C#

namespace LaaProduction.Web.Controllers
{
using LaaProductionHttp.Interfaces;
using LaaProduction.Web.App_Infrastructure;
using LaaProductionDI;
using System.Threading.Tasks;
using System.Web.Mvc;
[AllowAnonymous]
public class HomeController : Controller
{
private readonly IHttpClient httpClient;
public HomeController()
{
this.httpClient = LaaServiceProvider.GetService<IHttpClient>();
}
[HttpGet]
public ActionResult Index()
=> this.View();
[HttpPost]
public async Task<ActionResult> Index(string username, string password)
{
if (string.IsNullOrWhiteSpace(username))
{
this.ModelState.AddModelError(nameof(username), "Benutzername ist erforderlich.");
}
if (string.IsNullOrWhiteSpace(password))
{
this.ModelState.AddModelError(nameof(password), "Kennwort ist erforderlich.");
}
password = password.ComputeHash();
var software = typeof(HomeController)
.Assembly
.GetName();
var assembly = software.Name;
var majorV = software.Version.Major;
var minorV = software.Version.Minor;
var buildV = software.Version.Build;
var token = await this.httpClient
.Post("/Login/Local")
.WhitJsonBody(new { username, password, assembly, majorV, minorV, buildV })
.SendAsync<string>();
if (!token.Succeeded)
{
foreach (var error in token.Errors)
{
this.ModelState.AddModelError(error.Key, error.Value);
}
if (!string.IsNullOrWhiteSpace(token.Message))
{
this.ModelState.AddModelError(nameof(token.Message), token.Message);
}
}
else
{
this.HttpContext.SignIn(token.Value);
return this.Redirect(this.HttpContext.GetUrlReferer());
}
return this.View();
}
public ActionResult Logout()
{
this.HttpContext.SignOut();
return this.Redirect("~/");
}
}
}