Advertisement
MadCortez

Untitled

Jun 27th, 2023
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using Microsoft.AspNetCore.Authentication.Cookies;
  2. using Microsoft.AspNetCore.Authentication;
  3. using Microsoft.AspNetCore.Mvc;
  4. using MyCloud.ViewModels.Account;
  5. using System.Security.Claims;
  6. using MyCloud.Interfaces;
  7. using System.Net;
  8. using Microsoft.AspNetCore.Authorization;
  9.  
  10. namespace MyCloud.Controllers
  11. {
  12.     public class AccountController : Controller
  13.     {
  14.         private readonly IAccount _accountService;
  15.  
  16.         public AccountController(IAccount accountService)
  17.         {
  18.             _accountService = accountService;
  19.         }
  20.  
  21.         [HttpGet]
  22.         public IActionResult Register() => View();
  23.  
  24.         [HttpPost]
  25.         public async Task<IActionResult> Register(RegisterViewModel model)
  26.         {
  27.             if (ModelState.IsValid)
  28.             {
  29.                 var response = await _accountService.Register(model);
  30.                 if (response.StatusCode == HttpStatusCode.OK)
  31.                 {
  32.                     await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
  33.                         new ClaimsPrincipal(response.Data));
  34.  
  35.                     return RedirectToAction("Index", "Home");
  36.                 }
  37.                 ModelState.AddModelError("", response.Description);
  38.             }
  39.             return View(model);
  40.         }
  41.  
  42.         [HttpGet]
  43.         public IActionResult Login() => View();
  44.  
  45.         [HttpPost]
  46.         public async Task<IActionResult> Login(LoginViewModel model)
  47.         {
  48.             if (ModelState.IsValid)
  49.             {
  50.                 var response = await _accountService.Login(model);
  51.                 if (response.StatusCode == HttpStatusCode.OK)
  52.                 {
  53.                     await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
  54.                         new ClaimsPrincipal(response.Data));
  55.  
  56.                     return RedirectToAction("Index", "Home");
  57.                 }
  58.                 ModelState.AddModelError("", response.Description);
  59.             }
  60.             return View(model);
  61.         }
  62.  
  63.         [ValidateAntiForgeryToken]
  64.         public async Task<IActionResult> Logout()
  65.         {
  66.             await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
  67.             return RedirectToAction("Index", "Home");
  68.         }
  69.  
  70.         [Authorize(Roles = "Admin")]
  71.         public async Task<IActionResult> GetUsers()
  72.         {
  73.             var response = await _accountService.GetUsers();
  74.             if (response.StatusCode == HttpStatusCode.OK)
  75.             {
  76.                 return View(response.Data);
  77.             }
  78.             return RedirectToAction("Index", "Home");
  79.         }
  80.  
  81.         [Authorize(Roles = "Admin")]
  82.         public async Task<IActionResult> DeleteUser(long id, string name)
  83.         {
  84.             var response = await _accountService.DeleteUser(id);
  85.             if (response.StatusCode == HttpStatusCode.OK)
  86.             {
  87.                 string dirPath = $"wwwroot/Files/{name}";
  88.                 System.IO.Directory.Delete(dirPath, true);
  89.                 return RedirectToAction("GetUsers");
  90.             }
  91.             return RedirectToAction("Index", "Home");
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement