Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Authorize]
- public class AccountController : Controller
- {
- private UserService _userService;
- public UserService UserService{
- get { return _userService ?? (_userService = new UserService()); }
- }
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginViewModel model, string returnUrl){
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- //the line with SignInManager is Default in project
- //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
- //I have implemented my User service which checks in DB is there exists such a user with email and password and returns the same SignInStatus
- var result = UserService.Authenticate(model.Email, model.Password);
- switch (result)
- {
- case SignInStatus.Success:
- return RedirectToLocal(returnUrl);
- case SignInStatus.LockedOut:
- return View("Lockout");
- case SignInStatus.RequiresVerification:
- return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
- case SignInStatus.Failure:
- default:
- ModelState.AddModelError("", "Invalid login attempt.");
- return View(model);
- }
- }
- }
- public class UserService : IUserService
- {
- public SignInStatus Authenticate(string email, string password)
- {
- if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
- {
- return SignInStatus.Failure;
- }
- //TODO: perform authentication against DB account
- if (email == "mymail@mail.com" && password == "123")
- {
- return SignInStatus.Success;
- }
- else
- {
- return SignInStatus.Failure;
- }
- }
- }
- public class AdministrationController : Controller
- {
- // GET: Admin/Admin
- [Authorize]
- public ActionResult Index()
- {
- return View();
- }
- }
- public class AdministrationController : Controller
- {
- // GET: Admin/Admin
- [Authorize(Roles = "Administrator")]
- public ActionResult Index()
- {
- return View();
- }
- }
- case SignInStatus.Success:
- var user = new ApplicationUser
- {
- Email = model.Email,
- UserName = model.Email,
- ... set any other properties that you find convenient
- };
- await SignInManager.SignInAsync(user, false, false);
- return RedirectToLocal(returnUrl);
Add Comment
Please, Sign In to add comment