Michele: Model-View-Controller per Login + Classe SessionHelper + Model-View-Controller per Portale
This commit is contained in:
parent
18bde9852d
commit
583e123ef3
80
Controllers/LoginController.cs
Normal file
80
Controllers/LoginController.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using SoftwayWeb.Models;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SoftwayWeb.Controllers
|
||||||
|
{
|
||||||
|
public class LoginController : Controller
|
||||||
|
{
|
||||||
|
string apiUrl = string.Empty;
|
||||||
|
HttpClient client;
|
||||||
|
SessionHelper helper;
|
||||||
|
string errMes = string.Empty;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
public LoginController(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
client = new HttpClient();
|
||||||
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
||||||
|
apiUrl = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult Login()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Login(Login model)
|
||||||
|
{
|
||||||
|
if (ModelState.IsValid)
|
||||||
|
{
|
||||||
|
helper = new SessionHelper(this);
|
||||||
|
string url = apiUrl + "Login/loginMagazzino";
|
||||||
|
Uri baseAddress = new Uri(url);
|
||||||
|
client.BaseAddress = baseAddress;
|
||||||
|
//ViewBag.Error = string.Empty;
|
||||||
|
//ViewBag.Admin = string.Empty;
|
||||||
|
|
||||||
|
Login_Out loginOutput = new Login_Out();
|
||||||
|
string data = JsonConvert.SerializeObject(model);
|
||||||
|
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
|
||||||
|
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
string risultato = response.Content.ReadAsStringAsync().Result;
|
||||||
|
loginOutput = JsonConvert.DeserializeObject<Login_Out>(risultato);
|
||||||
|
|
||||||
|
return RedirectToAction("Index", "Portale");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errMes = response.Content.ReadAsStringAsync().Result;
|
||||||
|
loginOutput = JsonConvert.DeserializeObject<Login_Out>(errMes);
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
//return View(model);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var Elemento in ModelState.Values)
|
||||||
|
{
|
||||||
|
foreach (var Errore in Elemento.Errors)
|
||||||
|
{
|
||||||
|
string ErroreRilevato = Errore.ErrorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Controllers/PortaleController.cs
Normal file
12
Controllers/PortaleController.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace SoftwayWeb.Controllers
|
||||||
|
{
|
||||||
|
public class PortaleController : Controller
|
||||||
|
{
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Models/Login.cs
Normal file
12
Models/Login.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace SoftwayWeb.Models
|
||||||
|
{
|
||||||
|
public class Login
|
||||||
|
{
|
||||||
|
[Display(Name = "Username"), Required(ErrorMessage = "Username obbligatorio")]
|
||||||
|
public string? username { get; set; }
|
||||||
|
[Display(Name = "Password"), Required(ErrorMessage = "Password obbligatoria")]
|
||||||
|
public string? password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
26
Models/Login_Out.cs
Normal file
26
Models/Login_Out.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
namespace SoftwayWeb.Models
|
||||||
|
{
|
||||||
|
public class Login_Out
|
||||||
|
{
|
||||||
|
/// <summary>token</summary>
|
||||||
|
public string? Tok { get; set; }
|
||||||
|
|
||||||
|
/// <summary>token</summary>
|
||||||
|
public string? Tccodice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>token</summary>
|
||||||
|
public string? Tcdescri { get; set; }
|
||||||
|
|
||||||
|
/// <summary>ruolo</summary>
|
||||||
|
public string? Tcruolo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Error Message</summary>
|
||||||
|
public string? err_title { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Error Message detail</summary>
|
||||||
|
public string? err_detail { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Status</summary>
|
||||||
|
public string? err_status_code { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
64
SessionHelper.cs
Normal file
64
SessionHelper.cs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SoftwayWeb
|
||||||
|
{
|
||||||
|
public class SessionHelper
|
||||||
|
{
|
||||||
|
private Controller _controller;
|
||||||
|
|
||||||
|
public SessionHelper(Controller controller)
|
||||||
|
{
|
||||||
|
_controller = controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetStringValue(string key)
|
||||||
|
{
|
||||||
|
return _controller.HttpContext.Session.GetString(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetStringValue(string key, string value)
|
||||||
|
{
|
||||||
|
|
||||||
|
_controller.HttpContext.Session.SetString(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int? GetInt32Value(string key)
|
||||||
|
{
|
||||||
|
return _controller.HttpContext.Session.GetInt32(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetInt32Value(string key, int value)
|
||||||
|
{
|
||||||
|
|
||||||
|
_controller.HttpContext.Session.SetInt32(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte[] GetBytesValue(string key)
|
||||||
|
{
|
||||||
|
return _controller.HttpContext.Session.Get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetBytesValue(string key, Byte[] value)
|
||||||
|
{
|
||||||
|
|
||||||
|
_controller.HttpContext.Session.Set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetStringAsBytesValue(string key, string value)
|
||||||
|
{
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(value);
|
||||||
|
_controller.HttpContext.Session.Set(key, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetStringFromBytesValue(string key)
|
||||||
|
{
|
||||||
|
var bytes = _controller.HttpContext.Session.Get(key);
|
||||||
|
return Encoding.UTF8.GetString(bytes);
|
||||||
|
}
|
||||||
|
public void ClearFormatedKey(string formatedKey)
|
||||||
|
{
|
||||||
|
_controller.HttpContext.Session.Remove(formatedKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,4 +6,8 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
44
Views/Login/Login.cshtml
Normal file
44
Views/Login/Login.cshtml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
@* @model IEnumerable<SoftwayWeb.Models.Login> *@
|
||||||
|
@model SoftwayWeb.Models.Login
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Login";
|
||||||
|
}
|
||||||
|
|
||||||
|
<h1>Login</h1>
|
||||||
|
|
||||||
|
<div class="col-lg-8 col-md-12 col-sm-12 col-12">
|
||||||
|
<div class="agy-contact-form">
|
||||||
|
@* <h4 class="agy-sub-heading">Login</h4> *@
|
||||||
|
<form asp-action="Login">
|
||||||
|
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
|
||||||
|
<div class="agy-field-holder">
|
||||||
|
<input asp-for="username" class="agy-form-field require" placeholder="Username" />
|
||||||
|
<span asp-validation-for="username" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-6 col-md-6 col-sm-12 col-12"> </div>
|
||||||
|
<br />
|
||||||
|
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
|
||||||
|
<div class="agy-field-holder">
|
||||||
|
<input asp-for="password" class="agy-form-field require" placeholder="Password" />
|
||||||
|
<span asp-validation-for="password" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
<div class="col-lg-12 col-md-12 col-sm-12 col-12">
|
||||||
|
<input type="submit" value="Login" class="agy-btn submitForm" />
|
||||||
|
<div class="response"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-4" style="color:red;">
|
||||||
|
@ViewBag.Error
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
8
Views/Portale/Index.cshtml
Normal file
8
Views/Portale/Index.cshtml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
@*
|
||||||
|
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
||||||
|
|
||||||
|
*@
|
||||||
|
@{
|
||||||
|
Layout = "~/Views/Shared/_LayoutAreaRis.cshtml";
|
||||||
|
}
|
||||||
|
Area riservata dopo login
|
||||||
@ -25,6 +25,9 @@
|
|||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
|
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link text-dark" asp-area="" asp-controller="Login" asp-action="Login">Accedi</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
51
Views/Shared/_LayoutAreaRis.cshtml
Normal file
51
Views/Shared/_LayoutAreaRis.cshtml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>@ViewData["Title"] - SoftwayWeb</title>
|
||||||
|
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
|
||||||
|
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
|
||||||
|
<link rel="stylesheet" href="~/SoftwayWeb.styles.css" asp-append-version="true" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SoftwayWeb</a>
|
||||||
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
|
||||||
|
aria-expanded="false" aria-label="Toggle navigation">
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
|
||||||
|
<ul class="navbar-nav flex-grow-1">
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Torna a Home</a>
|
||||||
|
</li>
|
||||||
|
@* <li class="nav-item">
|
||||||
|
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link text-dark" asp-area="" asp-controller="Login" asp-action="Login">Accedi</a>
|
||||||
|
</li> *@
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<div class="container">
|
||||||
|
<main role="main" class="pb-3">
|
||||||
|
@RenderBody()
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<footer class="border-top footer text-muted">
|
||||||
|
<div class="container">
|
||||||
|
© 2024 - SoftwayWeb - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
<script src="~/lib/jquery/dist/jquery.min.js"></script>
|
||||||
|
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
<script src="~/js/site.js" asp-append-version="true"></script>
|
||||||
|
@await RenderSectionAsync("Scripts", required: false)
|
||||||
|
</body>
|
||||||
@ -5,5 +5,10 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"ApplicationInsights": {
|
||||||
|
"rootUrlApi": "https://api.poloinformatico.it:8000/api/"
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user