Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. public ActionResult Login()
  2. {
  3. return View();
  4. }
  5.  
  6. [HttpPost]
  7. public ActionResult Login(LoginModel model, string returnUrl)
  8. {
  9. if (!ModelState.IsValid)
  10. return View(model);
  11.  
  12. var password = PasswordHash.Encrypt(model.Password);
  13.  
  14. var user = _userManagerService.GetUser(model.Username, password);
  15.  
  16. if (user != null)
  17. {
  18. FormsAuthHelper.SetAuthenticationCookie(Response, user);
  19.  
  20. if (returnUrl.Length > 1)
  21. {
  22. return Redirect(returnUrl);
  23. }
  24.  
  25. return RedirectToAction("Index", "Invoice");
  26. }
  27.  
  28. ModelState.AddModelError("", "The user name or password provided is incorrect.");
  29. // If we got this far, something failed, redisplay form
  30. return View(model);
  31. }
  32.  
  33. public class AutorizedOnly : AuthorizeAttribute
  34. {
  35.  
  36. private const string LOGIN_URL = "~/Accounting/Login";
  37.  
  38. public override void OnAuthorization(AuthorizationContext filterContext) {
  39. base.OnAuthorization(filterContext);
  40.  
  41. if (/*is NOT autorized*/) {
  42. Uri returnTo = filterContext.HttpContext.Request.UrlReferrer;
  43. filterContext.Result = new RedirectResult(LOGIN_URL, new { returnUrl = returnTo });
  44. }
  45. }
  46. }
  47.  
  48.  
  49. public class Accounting : Controller {
  50. ...
  51.  
  52. public IActionResul Login(Uri returnTo, SignInModel model) {
  53. if (ModelState.IsValid) {
  54. if (/*login success*/) {
  55. /* Set cookie with session id */
  56. return Redirect(returnTo.ToString());
  57. }
  58. }
  59. ...
  60. }
  61.  
  62. ...
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement