public async Task <ActionResult> Login(UserLoginCredentials aModel) { _services = new UserServices(); var _LoginResult = await _services.GetProviderName(aModel.Code, aModel.Password); if (_LoginResult == null) { ModelState.AddModelError("", "Error"); return(View("Login")); } /*Codigo nuevo*/ //Session["GymID"] = _LoginResult.GymId; //var authTicket = new FormsAuthenticationTicket( // 1, // version // aModel.Email, // user name // DateTime.Now, // created // DateTime.Now.AddMinutes(20), // expires // true, // persistent? // _LoginResult.RoleData[0].Name // can be used to store roles // ); //string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); //System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); Session["UserSession"] = aModel.Code; ///*FIN Codigo nuevo*/ //SecurityUtils.SetUserAuthenticated(_LoginResult); return(RedirectToAction("Index", "ProductChange")); }
public ActionResult SignIn() { ViewBag.LoginError = false; ViewBag.Title = "Login"; UserLoginCredentials credentials = new UserLoginCredentials(); return(View(credentials)); }
public async Task <ActionResult <string> > GetToken([FromBody] UserLoginCredentials loginCredentials) { var user = DBContext.User.SingleOrDefault(u => u.Email == loginCredentials.Email && u.Password == loginCredentials.Password); if (user == null) { return(NotFound("User not found")); } return(this.AuthService.GenerateToken(user)); }
public ActionResult Create(UserLoginCredentials model) { ViewBag.LoginError = false; if (!ModelState.IsValid) { return(View(model)); } if (model.Password != model.PasswordConfirm) { ModelState.AddModelError("", "Senha e Confirmação de senhas não estão iguais."); return(View("SignIn", model)); } using (UserBusiness userBusiness = new UserBusiness()) { GTUser user = userBusiness.GetUserByLogin(model.Login); if (user != null) { ModelState.AddModelError("", string.Format("Usuário já está cadastrado na base com o email '{0}'.", user.Email)); return(View("SignIn", model)); } user = new GTUser() { Email = model.Email, Login = model.Login, Name = model.Name, Password = model.Password }; userBusiness.CreateUser(user); user = userBusiness.GetUserByLogin(model.Login); //Checa por erros de domínio if (this.CheckForDomainErrors(userBusiness)) { ViewBag.LoginError = true; return(View("SignIn", model)); } //Salva o estado do usuário em sessão this.SetGTUser(user); } //Cria um cookie de autenticação para manter a sessão do usuário FormsAuthentication.SetAuthCookie(model.Login, false); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Login(UserLoginCredentials credentials) { var user = await _userService.GetOneByEmailAsync(credentials.Email); if (user == null) { return(NotFound()); } if (user.Password != credentials.Password) { return(Unauthorized()); } var token = await _jwtTokenGenerator.CreateToken(user.Id); return(Ok(token)); }
public async Task <IActionResult> Login([FromBody] UserLoginCredentials userLoginCredentials) { var user = await _userManager.FindByEmailAsync(userLoginCredentials.userEmail); if (user != null && await _userManager.CheckPasswordAsync(user, userLoginCredentials.password)) { //get the role of a user var roles = await _userManager.GetRolesAsync(user); var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_appsettings.Secret)); //prepare token details var tokenHandler = new JwtSecurityTokenHandler(); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(JwtRegisteredClaimNames.Sub, userLoginCredentials.userEmail), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(ClaimTypes.NameIdentifier, user.UserName), new Claim(ClaimTypes.Role, roles.FirstOrDefault()), new Claim("LoggedOn", DateTime.Now.ToString()) }), SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256), Issuer = _appsettings.Site, Audience = _appsettings.Audience, Expires = DateTime.UtcNow.AddDays(_appsettings.ExpireTimeDays) }; var token = tokenHandler.CreateToken(tokenDescriptor); return(Ok(new { AccessToken = tokenHandler.WriteToken(token), expiration = token.ValidTo, userEmail = user.Email, userRole = roles.FirstOrDefault().ToString() })); } return(Unauthorized()); }
public ActionResult SignIn(UserLoginCredentials model, string returnUrl) { ViewBag.LoginError = false; if (!ModelState.IsValid) { return(View(model)); } using (UserBusiness userBusiness = new UserBusiness()) { GTUser user = null; if (userBusiness.AuthenticateUser(model.Login, model.Password)) { user = userBusiness.GetUserByLogin(model.Login); } //Checa por erros de domínio if (this.CheckForDomainErrors(userBusiness)) { ViewBag.LoginError = true; return(View(model)); } //Salva o estado do usuário em sessão this.SetGTUser(user); } //Cria um cookie de autenticação para manter a sessão do usuário FormsAuthentication.SetAuthCookie(model.Login, false); if (!String.IsNullOrWhiteSpace(returnUrl)) { return(Redirect(returnUrl)); } return(RedirectToAction("Index", "Home")); }