Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override void ExecuteCore()
- {
- if (User.Identity.IsAuthenticated)
- {
- try
- {
- AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
- // set the current user.
- CurrentUser = AccountDataContext.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
- AccountDataContext.CurrentAccount = CurrentUser.Account;
- ViewBag.CurrentUser = CurrentUser;
- ViewBag.Account = CurrentUser.Account;
- SystemDataContext = new SystemDAL.DataContext(ConfigurationManager.AppSettings["Server"], CurrentUser.Account.Database);
- // setup the account based on the users settings
- ViewBag.Theme = "Default"; // hard coded for now
- }
- catch (Exception)
- {
- // if the previous threw an exception, then the logged in user has been deleted
- // log them out
- FormsAuthentication.SignOut();
- Session.Abandon();
- // clear the authentication cookie
- var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
- cookie.Expires = DateTime.Now.AddYears(-1);
- Response.Cookies.Add(cookie);
- FormsAuthentication.RedirectToLoginPage();
- }
- }
- base.ExecuteCore();
- }
- [AllowAnonymous]
- public ActionResult Login(string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- return View();
- }
- //
- // POST: /Account/Login
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Login(LoginViewModel model, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- if(AccountDataContext == null)
- AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
- var user = AccountDataContext.Users.FirstOrDefault(x => x.Email == model.UserName && x.Password == model.Password);
- if (user != null)
- {
- FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
- return RedirectToLocal(returnUrl);
- }
- else
- {
- ModelState.AddModelError("", "Invalid username or password.");
- }
- }
- // If we got this far, something failed, redisplay form
- return View(model);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement