public ActionResult Register(UserModel user) { if (ModelState.IsValid) { if (repository.IsExist(user.xUserName)) { var userVerify = repository.GetUser(user.xUserName); if (userVerify.xSenha == null) { user.idUsuario = userVerify.idUsuario; } else { this.aviso = "Usuário ja existente."; return View(user); } } if (repository.Save(user: user)) { this.SessionUserModel = repository.GetUser(user.xUserName); return RedirectToAction("Home", "Home"); } } else { this.aviso = "Dados estão incorretos."; } return View(user); }
public ActionResult LogIn(UserModel user) { try { if (repository.IsExist(xUserName: user.xUserName, xSenha: user.xSenha)) { this.SessionUserModel = repository.GetUser(user.xUserName); return RedirectToAction("Home", "Home"); } else { ModelState.AddModelError("", "Detalhes do login estão errados."); } return View(user); } catch (Exception ex) { throw ex; } }
public UserModel GetUser(string xUserName) { try { UserModel user = null; using (var db = new PortalEntities()) { var dados = db.tb_usuario.FirstOrDefault(c => c.xUserName == xUserName); if (dados != null) { user = new UserModel { idUsuario = dados.idUsuario, xFullName = dados.xFullName, xSenha = dados.xSenha, xUserName = dados.xUserName }; if (dados.tb_providers.Count() > 0) { dados.tb_providers.ToList().ForEach((c) => { user.providers.Add(new ProvidersModel { idUsuario = Convert.ToInt32(c.idUsuario), Provider = c.Provider, ProviderUserId = c.ProviderUserId }); }); } } } return user; } catch (Exception ex) { throw ex; } }
private static void SetPassword(UserModel user, tb_usuario u) { if (user.xSenha != null) { var crypto = new SimpleCrypto.PBKDF2(); var encrypPass = crypto.Compute(user.xSenha); u.xSenha = encrypPass; u.xSenhaSalt = crypto.Salt; } }
public bool Save(UserModel user) { try { using (var db = new PortalEntities()) { tb_usuario u = new tb_usuario(); if (user.idUsuario == null) { SetPassword(user, u); u.xUserName = user.xUserName; u.xFullName = user.xFullName; db.tb_usuario.Add(u); foreach (var prov in user.providers) { u.tb_providers.Add(new tb_providers { idUsuario = u.idUsuario, Provider = prov.Provider, ProviderUserId = prov.ProviderUserId }); } } else { u = db.tb_usuario.FirstOrDefault(c => c.xUserName == user.xUserName); SetPassword(user, u); u.xUserName = user.xUserName; u.xFullName = user.xFullName; foreach (var prov in user.providers) { tb_providers provider = null; if (u.tb_providers.Where(c => c.ProviderUserId == prov.ProviderUserId).Count() > 0) { provider = u.tb_providers.FirstOrDefault(c => c.ProviderUserId == prov.ProviderUserId); provider.ProviderUserId = prov.ProviderUserId; provider.Provider = prov.Provider; provider.idUsuario = prov.idUsuario; } else { provider = new tb_providers(); provider.ProviderUserId = prov.ProviderUserId; provider.Provider = prov.Provider; provider.idUsuario = prov.idUsuario; u.tb_providers.Add(provider); } } } db.SaveChanges(); } return true; } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
public ActionResult Recovery(UserModel user) { return View(); }
public ActionResult ExternalLoginConfirmation(UserModel model, string returnUrl) { return View(model); }
public ActionResult ExternalLoginCallback(string returnUrl) { AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); if (!result.IsSuccessful) { this.aviso = "Ocorreu um erro ao utilizar o login externo."; } UserModel user = null; if (!User.Identity.IsAuthenticated) { //user.xUserName = result.ExtraData.FirstOrDefault(x => x.Key == "username").Value; //user.idSocial = result.ExtraData.FirstOrDefault(x => x.Key == "id").Value; //user.ExternalLoginData = loginData; string xFullName = result.ExtraData.FirstOrDefault(x => x.Key == "name").Value; string userName = result.ExtraData.FirstOrDefault(x => x.Key == "username").Value; string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId); string provider = null; string providerUserId = null; if (OAuthWebSecurity.TryDeserializeProviderUserId(loginData, out provider, out providerUserId)) { if (providerUserId == result.ProviderUserId) { if (repository.IsExist(xUserName: userName)) { user = repository.GetUser(xUserName: userName); if (user != null) { if (user.providers.Where(c => c.Provider == provider && c.ProviderUserId == providerUserId).Count() == 0) { user.providers.Add(new ProvidersModel { idUsuario = Convert.ToInt32(user.idUsuario), Provider = provider, ProviderUserId = providerUserId }); } } } else { user = new UserModel { xFullName = xFullName, xUserName = userName, }; user.providers.Add(new ProvidersModel { Provider = provider, ProviderUserId = providerUserId }); } repository.Save(user); this.SessionUserModel = repository.GetUser(user.xUserName); ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName; ViewBag.ReturnUrl = returnUrl; } } } return RedirectToAction(actionName: "Home", controllerName: "Home"); }