Aliendreamer

login asp net core view

Nov 18th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. namespace Eventures.WebApp.Controllers
  2. {
  3.     using System.Threading.Tasks;
  4.     using Data;
  5.     using Models;
  6.     using ViewModels;
  7.     using Microsoft.AspNetCore.Identity;
  8.     using Microsoft.AspNetCore.Mvc;
  9.     using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
  10.  
  11.     public class UsersController : BaseController
  12.     {
  13.         private SignInManager<EventureUser> SignInManager { get; }
  14.  
  15.         public UsersController(SignInManager<EventureUser> signInManager, ApplicationDbContext context)
  16.             : base(context)
  17.         {
  18.             this.SignInManager = signInManager;
  19.         }
  20.  
  21.         [HttpGet]
  22.         public IActionResult Login()
  23.         {
  24.             return this.View();
  25.         }
  26.  
  27.         [HttpPost]
  28.         public async Task<IActionResult> Login(LoginInputModel model)
  29.         {
  30.             var result = await this.SignInManager.PasswordSignInAsync(model.Username,
  31.                 model.Password, true, false);
  32.  
  33.             if (result == SignInResult.Success)
  34.             {
  35.                 return RedirectToAction("Index", "Home");
  36.             }
  37.  
  38.             this.ViewData["Error"] = "No such user";
  39.             return this.View();
  40.         }
  41.  
  42.         [HttpGet]
  43.         public IActionResult Register()
  44.         {
  45.             return this.View();
  46.         }
  47.  
  48.         [HttpPost]
  49.         public async Task<IActionResult> Register(RegisterInputModel model)
  50.         {
  51.             var user = new EventureUser
  52.             {
  53.                 UserName = model.Username,
  54.                 FirstName = model.Firstname,
  55.                 Email = model.Email,
  56.                 LastName = model.Email,
  57.                 UniqueCitizenNumber = model.UniqueCitizenNumber
  58.             };
  59.  
  60.             await this.SignInManager.UserManager.CreateAsync(user, model.Password);
  61.  
  62.             var result = await this.SignInManager.PasswordSignInAsync(model.Username,
  63.                 model.Password, true, false);
  64.  
  65.             if (result == SignInResult.Success)
  66.             {
  67.                 return RedirectToAction("Index", "Home");
  68.             }
  69.  
  70.             return this.View();
  71.         }
  72.  
  73.         [HttpGet]
  74.         public async Task<IActionResult> Logout()
  75.         {
  76.             await this.SignInManager.SignOutAsync();
  77.  
  78.             return RedirectToAction("Index", "Home");
  79.         }
  80.  
  81.         [HttpGet]
  82.         public IActionResult ForgottenPassword()
  83.         {
  84.             return NotFound();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment