public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } Session session = new Session(); try { session = await userApi.LoginAsync(model.Email, model.Password); } catch (DreamFactoryException) { try { session = await adminApi.LoginAdminAsync(model.Email, model.Password); } catch {; } } if (string.IsNullOrEmpty(session.SessionId)) { ModelState.AddModelError("", "Invalid login attempt."); return View(model); } SignIn(session, model.RememberMe); return RedirectToLocal(returnUrl); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { Register register = new Register { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Name = model.Name, NewPassword = model.Password }; bool result; try { result = await userApi.RegisterAsync(register); } catch (DreamFactoryException) { result = false; } if (result) { Session session = new Session(); try { session = await userApi.LoginAsync(model.Email, model.Password); } catch (DreamFactoryException) {;} if (string.IsNullOrEmpty(session.SessionId)) { return RedirectToAction("Login", "Account"); } SignIn(session, false); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "There has been an error registering your account."); } } return View(model); }
private void SignIn(Session session, bool rememberMe) { List<Claim> claims = new List<Claim> { new Claim(ClaimTypes.Name, session.Name), new Claim(ClaimTypes.NameIdentifier, session.Id), }; if (session.IsSysAdmin ?? false) { claims.Add(new Claim(ClaimTypes.Role, DreamFactoryContext.Roles.SysAdmin)); } ClaimsIdentity identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationType); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity); }