public async static Task <UcabUserAuth> AuthenticateUser(string usuario, string pass) { try { UcabUserAuth result = await Startup._configuration["appSettings:URLBANNERRESTAPI"] .AppendPathSegment("v1") .AppendPathSegment("ucab") .AppendPathSegment("users") .AppendPathSegment("auth/") .WithBasicAuth(userBanner, passwordBanner) .PostJsonAsync ( new { uid = usuario, password = pass } ) .ReceiveJson <UcabUserAuth>(); return(result); } catch (FlurlHttpException e) { Console.WriteLine("Error de Flurl al validar credenciales de Usuario -> {user}", usuario); Console.WriteLine("Error -> {Message}", e.Message); return(null); } catch (Exception e) { Console.WriteLine("Error al consumir servicio de Banner -> {0}", Startup._configuration["appSettings:URLBANNERRESTAPI"]); Console.WriteLine("Error -> {Message}", e.Message); return(null); } }
public async Task <IActionResult> Login(LoginViewModel loginViewModel) { UcabUserAuth userAuth = await BannerRestAPI.AuthenticateUser(loginViewModel.username, loginViewModel.password); if (userAuth == null) { return(Redirect(loginViewModel.returnUrl)); } if (userAuth.success) { var user = new IdentityUser(loginViewModel.username); var resultUser = await _userManager.CreateAsync(user, loginViewModel.password); if (resultUser.Succeeded) { var resultClaim = await _userManager.AddClaimAsync(user, new Claim("uid", userAuth.uid)); if (resultClaim.Succeeded) { var resultSignIn = await _signInManager.PasswordSignInAsync(user, loginViewModel.password, false, false); if (resultSignIn.Succeeded) { return(Redirect(loginViewModel.returnUrl)); } } } else { var resultSignIn = await _signInManager.PasswordSignInAsync(user, loginViewModel.password, false, false); if (resultSignIn.Succeeded) { return(View(loginViewModel)); } } } else { return(Redirect(loginViewModel.returnUrl)); } return(View(loginViewModel)); }