public ActionResult Login(LoginCredentials credentials) { var client = new SMARestClient("SessionService.svc"); Session newSession = null; try { newSession = client.Post<LoginCredentials, Session>("/login", credentials); } catch { newSession = null; } if(newSession == null) { ViewBag.Message = "Login failed. Please try again."; ViewBag.MessageClass = "text-danger"; return View(); } else { var user = new SMARestClient("UserService.svc").Get<User>($"/user/{newSession.UserID}"); if (user != null) System.Web.HttpContext.Current.Session["logged_in_user_obj"] = user; System.Web.HttpContext.Current.Session["auth_token"] = newSession.Token; return RedirectToAction("Index", "Dashboard"); } }
public Session Login(LoginCredentials credentials) { if (auth.ValidateCredentials(credentials.Email, credentials.Pwd)) { var user = userDA.GetOneByEmail(credentials.Email); var userSession = sessionDA.GetAll().Where(s => s.UserID == user.Id).FirstOrDefault(); Session session = null; if (userSession != null && auth.ValidateToken(userSession.Token)) { session = userSession; } else { session = new Session(user); } sessionDA.Insert(session); sessionDA.SaveChanges(); return session; } else { throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized); } }