Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <--LoginControler-->
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using WellnessAndSpaCentar.web.Models;
- using WellnessAndSpaCentar.web.ViewModels;
- using WellnessAndSpaCentar.web.Helper;
- using Microsoft.AspNetCore.Mvc.ModelBinding;
- using Microsoft.AspNetCore.Http;
- namespace WellnessAndSpaCentar.web.Controllers
- {
- public class LoginController : Controller
- {
- private Wellness___spa_centar___RSIContext ctx = new Wellness___spa_centar___RSIContext();
- public IActionResult LoginPage()
- {
- if (Authentication.GetLoggedUser(HttpContext) != null)
- return RedirectToAction("");
- return View("LoginPage", new LoginVM());
- }
- public IActionResult VerifyRequest(LoginVM model)
- {
- User user = ctx.User.Where(x => x.UserName == model.LoginData).FirstOrDefault();
- if(user == null)
- {
- ModelState.AddModelError("", "User does not exist!");
- }
- else
- {
- if(!Security.UserExist(model.LoginData, model.Password, user))
- {
- ModelState.AddModelError("", "Username or password is incorrect!");
- }
- }
- if (!ModelState.IsValid)
- {
- return View("LoginPage", model);
- }
- Authentication.NewSession(user, HttpContext); //httpContext je null
- return RedirectToAction("Index", "Home");
- }
- }
- }
- <--Folder: Helper, Klasa: Autentifikacija-->
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using WellnessAndSpaCentar.web.Models;
- namespace WellnessAndSpaCentar.web.Helper
- {
- public class Authentication
- {
- //Authentication is the act of confirming the truth of an attribute of a single piece of data claimed true by an entity.
- private const string _loggedUser = "logged_user";
- private IHttpContextAccessor _httpContextAccessor;
- public Authentication(IHttpContextAccessor httpContextAccessor)
- {
- this._httpContextAccessor = httpContextAccessor;
- }
- public static void NewSession(User user, HttpContext ctx)
- {
- ctx.Session.SetJson(_loggedUser, user);
- }
- public static void EndSession(User user, HttpContext ctx)
- {
- ctx.Session.SetJson(_loggedUser, null);
- // ukoliko ne bude prihvatao null koristiti ovu komandu -> ctx.Session.SetString(_loggedUser, "");
- }
- public static User GetLoggedUser(HttpContext ctx)
- {
- User user = ctx.Session.GetJson<User>(_loggedUser);
- if(user != null)
- {
- return user;
- }
- NewSession(user, ctx);
- return user;
- }
- }
- }
- <--folder Helper, klasa Autorizacija-->
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using WellnessAndSpaCentar.web.Models;
- namespace WellnessAndSpaCentar.web.Helper
- {
- public class Authorization
- {
- //Authorization is the function of specifying access rights/privileges to resources
- private readonly bool _allUsers;
- public Authorization(bool allUsers)
- {
- _allUsers = allUsers;
- }
- public void OnAuthoritazion(AuthorizationFilterContext context)
- {
- User user = Authentication.GetLoggedUser(context.HttpContext);
- if(user == null)
- {
- context.HttpContext.Response.Redirect("/Login/LoginPage");//neka akcija
- return;
- }
- if(_allUsers && user.Active)
- {
- return;
- }
- if(!_allUsers && user.Active && user.RoleId == 1) //provjera da li je administrator
- {
- return;
- }
- if (!_allUsers && user.Active && user.RoleId == 2) //provjera da li je employee
- {
- return;
- }
- if (!_allUsers && user.Active && user.RoleId == 3) //provjera da li je client
- {
- return;
- }
- if (!_allUsers && user.Active && user.RoleId == 4) //provjera da li je supplier
- {
- return;
- }
- context.HttpContext.Response.Redirect("/Login/LoginPage");
- }
- }
- }
- <--folder Helper, klasa Security-->
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using WellnessAndSpaCentar.web.Models;
- namespace WellnessAndSpaCentar.web.Helper
- {
- public class Security
- {
- public static bool UserExist(string username, string password, User user)
- {
- if(user.UserName == username && user.Password == password)
- {
- return true;
- }
- return false;
- }
- }
- }
- <--folder Helper, klasa Session-->
- using Microsoft.AspNetCore.Http;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace WellnessAndSpaCentar.web.Helper
- {
- public static class SessionExtension
- {
- public static void SetJson(this ISession session, string key, object value) => session.SetString(key, JsonConvert.SerializeObject(value));
- public static T GetJson<T>(this ISession session, string key) => session.GetString(key) == null ? default(T) : JsonConvert.DeserializeObject<T>(session.GetString(key));
- }
- }
- <-- folder Viewv, klasa LoginPage-->
- @{
- Layout = "_Layout";
- }
- @model WellnessAndSpaCentar.web.ViewModels.LoginVM
- <body>
- <div class="login-div">
- <div class="login-content">
- <img src="~/images/logo-white.png" alt="Logo" style="width:150px; height:150px;" />
- <h5 style="color:white; text-transform:uppercase; font-size:24px;">Wellness & spa center</h5>
- <form name="login-form" asp-action="VerifyRequest" method="post" onsubmit="validateForm()">
- <br />
- <input asp-for="LoginData" name="LoginData" class="form-control" placeholder="Username" autofocus /><br/>
- <input asp-for="Password" name="Password" class="form-control" placeholder="Password"/><br />
- <button type="submit" class="btn btn-primary">Login</button><br /><br />
- @if (!ViewData.ModelState.IsValid)
- {
- <div asp-validation-summary="All" class="alert alert-danger"> </div>
- }
- </form>
- </div>
- </div>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement