/// <summary> /// Divide el nombre del usuario que se obtiene del proveedor externo. /// </summary> /// <param name="model"></param> /// <param name="claimName"></param> protected void splitClaimName(LoginExternalConfirmViewModel model, Claim claimName) { string names = claimName != null ? claimName.Value : string.Empty; if (names != string.Empty) { var split = names.Split(new char[] { ' ' }); int counter = 0; foreach (string name in split) { if (counter < 1) { model.usuNombre += " " + name; } else { model.usuApellido += " " + name; } counter++; } } }
public async Task <ActionResult> LoginExternalConfirmAsync(string returnUrl = null) { ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Index", "Home", hasReturnUrl(returnUrl))); } //Tries to log in and if it has success redirects to admin page. string logInUrl = await externalLoginSignInSync(loginInfo, returnUrl); if (logInUrl != null) { return(Redirect(logInUrl)); } //Is not registered in the database register claims in the database and try to loggin again var claimName = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name); var claimEmail = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email); LoginExternalConfirmViewModel model = new LoginExternalConfirmViewModel(); splitClaimName(model, claimName); model.usuEmail = claimEmail != null ? claimEmail.Value : String.Empty; model.usuEmailConfirmar = model.usuEmail; model.LoginProvider = loginInfo.Login.LoginProvider; if (await externalLoginCreateUser_Provider(model, loginInfo)) { logInUrl = await externalLoginSignInSync(loginInfo, returnUrl); if (logInUrl != null) { return(Redirect(logInUrl)); } } return(RedirectToAction("Index", "Home")); }
/// <summary> /// Crea el usuario si se logea si este no existe en la bd y e inicio sesion desde un proveedor externo /// </summary> /// <param name="model"></param> /// <param name="loginInfo"></param> /// <returns></returns> protected async Task <bool> externalLoginCreateUser_Provider(LoginExternalConfirmViewModel model, ExternalLoginInfo loginInfo) { //Aqui crear la contraseña Users newpass = new Users(); PasswordHasher passhash = new PasswordHasher(); string pass = newpass.CreateRandomPassword(7); string pass2 = passhash.HashPassword(pass); ApplicationUser user = new ApplicationUser { UserName = model.usuEmail, Email = model.usuEmail, EmailConfirmed = true, PasswordHash = pass2 }; var userChecked = db.AspNetUsers.Where(s => s.Email == model.usuEmail).FirstOrDefault(); if (userChecked == null) { var result = await UserManager.CreateAsync(user); if (result.Succeeded) { Users users = new Users(); string idUser = users.IdUser(model.usuEmail); System.Web.HttpContext.Current.Session["SessionUser"] = idUser; users.EnvioCorreo(model.usuEmail, pass); users.UpdateRoleUser(model.usuEmail); var promo = users.CreateRandomPromoCode(); bool validatepromo = users.SavePromoCode(promo, user.Email); if (validatepromo == true) { } else { while (validatepromo == false) { promo = users.CreateRandomPromoCode(); validatepromo = users.SavePromoCode(promo, model.usuEmail); } } result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (result.Succeeded) { return(true); } else { Global_Functions.saveErrors(result.Errors.ToString(), false); AddErrors(result); } } else { Global_Functions.saveErrors(result.Errors.ToString(), false); AddErrors(result); } } else { user.Id = userChecked.Id; //Inserta en la base de datos para que ese usuario pueda ingresar por ese proveedor con la misma cuenta de correo. var result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (result.Succeeded) { return(true); } else { Global_Functions.saveErrors(result.Errors.ToString(), false); AddErrors(result); } } return(false); }