Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. protected override void ExecuteCore()
  2. {
  3.  
  4.  
  5.  
  6. if (User.Identity.IsAuthenticated)
  7. {
  8. try
  9. {
  10. AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
  11. // set the current user.
  12. CurrentUser = AccountDataContext.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
  13. AccountDataContext.CurrentAccount = CurrentUser.Account;
  14. ViewBag.CurrentUser = CurrentUser;
  15. ViewBag.Account = CurrentUser.Account;
  16.  
  17. SystemDataContext = new SystemDAL.DataContext(ConfigurationManager.AppSettings["Server"], CurrentUser.Account.Database);
  18.  
  19. // setup the account based on the users settings
  20. ViewBag.Theme = "Default"; // hard coded for now
  21. }
  22. catch (Exception)
  23. {
  24. // if the previous threw an exception, then the logged in user has been deleted
  25. // log them out
  26. FormsAuthentication.SignOut();
  27. Session.Abandon();
  28.  
  29. // clear the authentication cookie
  30. var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
  31. cookie.Expires = DateTime.Now.AddYears(-1);
  32. Response.Cookies.Add(cookie);
  33.  
  34. FormsAuthentication.RedirectToLoginPage();
  35. }
  36. }
  37.  
  38. base.ExecuteCore();
  39. }
  40.  
  41. [AllowAnonymous]
  42. public ActionResult Login(string returnUrl)
  43. {
  44. ViewBag.ReturnUrl = returnUrl;
  45. return View();
  46. }
  47.  
  48. //
  49. // POST: /Account/Login
  50. [HttpPost]
  51. [AllowAnonymous]
  52. [ValidateAntiForgeryToken]
  53. public ActionResult Login(LoginViewModel model, string returnUrl)
  54. {
  55. if (ModelState.IsValid)
  56. {
  57.  
  58. if(AccountDataContext == null)
  59. AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
  60.  
  61. var user = AccountDataContext.Users.FirstOrDefault(x => x.Email == model.UserName && x.Password == model.Password);
  62. if (user != null)
  63. {
  64. FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
  65. return RedirectToLocal(returnUrl);
  66. }
  67. else
  68. {
  69. ModelState.AddModelError("", "Invalid username or password.");
  70. }
  71. }
  72.  
  73. // If we got this far, something failed, redisplay form
  74. return View(model);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement