private async Task <CognitoUser> ChallengeUser(string username, string password) { AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(RegionEndpoint.USEast2); var request = new InitiateAuthRequest { ClientId = this.CLIENTAPP_ID, AuthFlow = AuthFlowType.CUSTOM_AUTH, }; request.AuthParameters.Add("USERNAME", username); request.AuthParameters.Add("PASSWORD", password); var response = await provider.InitiateAuthAsync(request).ConfigureAwait(false); if (response.AuthenticationResult != null) { return(createUser(username, provider)); } if (response.ChallengeName != null) { var selectAccount = new SelectAccount(username, response.Session, response.ChallengeParameters); selectAccount.ShowDialog(); return(createUser(username, provider)); } return(null); }
public async Task <InitiateAuthResponse> InitiateAuthAsync(ISigninCredential signinCredentialImp, CancellationToken cancellationToken = new CancellationToken()) { var request = new InitiateAuthRequest { ClientId = _clientId, AuthParameters = new Dictionary <string, string> { { "USERNAME", signinCredentialImp.Username }, { "PASSWORD", signinCredentialImp.Password } }, AuthFlow = AuthFlowType.USER_PASSWORD_AUTH }; return(await _client.InitiateAuthAsync(request, cancellationToken)); }
public AuthenticationResult LoginUser(LoginUserQueryParam loginUserQueryParam) { InitiateAuthRequest iaRequest = new InitiateAuthRequest { ClientId = _connectionInfo.ClientId, AuthFlow = AuthFlowType.USER_PASSWORD_AUTH }; iaRequest.AuthParameters.Add("USERNAME", loginUserQueryParam.Email); iaRequest.AuthParameters.Add("PASSWORD", loginUserQueryParam.Password); InitiateAuthResponse authResp = new InitiateAuthResponse(); var authResult = new AuthenticationResult(); try { authResp = _provider.InitiateAuthAsync(iaRequest).Result; authResult.AccessToken = authResp.AuthenticationResult.AccessToken; authResult.IdToken = authResp.AuthenticationResult.IdToken; authResult.RefreshToken = authResp.AuthenticationResult.RefreshToken; authResult.TokenType = authResp.AuthenticationResult.TokenType; authResult.ExpiresIn = authResp.AuthenticationResult.ExpiresIn; authResult.Success = true; } catch (AggregateException e) { e.Handle((x) => { if (x is UserNotConfirmedException) // This we know how to handle. { LoggingHandler.LogInfo("User not confirmed."); authResult.Success = false; authResult.ResponseMessage = "User not confirmed"; return(true); } if (x is NotAuthorizedException) // This we know how to handle. { LoggingHandler.LogInfo("Invalid credentials provided."); authResult.Success = false; authResult.ResponseMessage = "Invalid credentials provided."; return(true); } return(false); // Let anything else stop the application. }); } return(authResult); }
/// <summary> /// Create an auth token /// </summary> /// <returns></returns> public Task <InitiateAuthResponse> AuthAsync(string username, string password) { var initiateAuthRequest = new InitiateAuthRequest { ClientId = _clientId, AuthFlow = Amazon.CognitoIdentityProvider.AuthFlowType.USER_PASSWORD_AUTH, AuthParameters = new Dictionary <string, string>() { { "USERNAME", username }, { "PASSWORD", password }, } }; using (var client = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), _region)) { return(client.InitiateAuthAsync(initiateAuthRequest)); } }
public async Task <Tuple <int, string> > LoginUserAsync(string username, string password) { try { CurrentUsername = username; var authRequest = new InitiateAuthRequest { AuthFlow = AuthFlowType.USER_PASSWORD_AUTH, ClientId = Constants.POOL_CLIENT_ID }; authRequest.AuthParameters.Add("USERNAME", username); authRequest.AuthParameters.Add("PASSWORD", password); var authResp = await client.InitiateAuthAsync(authRequest); if (authResp.HttpStatusCode == HttpStatusCode.OK) { UserAccessToken = authResp.AuthenticationResult.AccessToken; return(Tuple.Create <int, string>(1, "Login Success!")); } } catch (UserNotConfirmedException e) { Console.WriteLine(e.Message); return(Tuple.Create <int, string>(2, e.Message)); } catch (NotAuthorizedException e) { Console.WriteLine(e.Message); return(Tuple.Create <int, string>(3, e.Message)); } catch (UserNotFoundException e) { Console.WriteLine(e.Message); return(Tuple.Create <int, string>(4, e.Message)); } catch (Exception e) { Console.WriteLine(e.Message); } return(Tuple.Create <int, string>(5, "Invalid username and passowrd!")); }
public override async Task <AuthenticationResultType> Handle(CognitoSignInCommand request, CancellationToken cancellationToken) { using (var provider = new AmazonCognitoIdentityProviderClient(AwsId, AwsKey, RegionEndpoint.USEast1)) { var response = await provider.InitiateAuthAsync(new InitiateAuthRequest { AuthFlow = AuthFlowType.USER_PASSWORD_AUTH, ClientId = UserGroupClientId, AuthParameters = new Dictionary <string, string> { { "USERNAME", request.Email }, { "PASSWORD", request.Password }, { "EMAIL", request.Email } } }, cancellationToken); response.AuthenticationResult.AccessToken = response.AuthenticationResult.IdToken; return(response.AuthenticationResult); } }
public async Task <LoginResponse> LoginAsync(string username, string password) { LoginResponse loginResponse; InitiateAuthRequest auth = new InitiateAuthRequest() { ClientId = CLIENT_ID, AuthFlow = AuthFlowType.USER_PASSWORD_AUTH, }; auth.AuthParameters.Add("USERNAME", username); auth.AuthParameters.Add("PASSWORD", password); InitiateAuthResponse response = await _loginClient.InitiateAuthAsync(auth); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); JwtSecurityToken jsonToken = handler.ReadToken(response.AuthenticationResult.IdToken) as JwtSecurityToken; loginResponse = new LoginResponse() { Email = jsonToken.Claims.Single(c => c.Type == "email").Value, EmailVerified = bool.Parse(jsonToken.Claims.Single(c => c.Type == "email_verified").Value), FamilyName = jsonToken.Claims.Single(c => c.Type == "family_name").Value, GivenName = jsonToken.Claims.Single(c => c.Type == "given_name").Value, HomeStudioId = jsonToken.Claims.Single(c => c.Type == "custom:home_studio_id").Value, IsMigration = bool.Parse(jsonToken.Claims.Single(c => c.Type == "custom:isMigration").Value), Locale = jsonToken.Claims.Single(c => c.Type == "locale").Value, MemberId = jsonToken.Claims.Single(c => c.Type == "cognito:username").Value, JwtToken = response.AuthenticationResult.IdToken, Expiration = jsonToken.ValidTo, IssuedOn = jsonToken.ValidFrom, }; } else { loginResponse = null; } return(loginResponse); }