Exemple #1
0
        public BooleanResult AuthenticateUser(SessionProperties properties)
        {
            // this method shall say if our credentials are valid
            UserInformation userInfo = properties.GetTrackedSingle <UserInformation>();

            return(Cognito.getResponse(userInfo.Username, userInfo.Password));
        }
        /// <summary>
        /// Start signup process for new user in Cognito user pool
        /// </summary>
        /// <param name="regReq"></param>
        /// <returns></returns>
        public async Task <SignUpResponse> RegisterUserAsync(RegistrationRequest regReq)
        {
            // Register the user using Cognito
            var signUpRequest = new SignUpRequest
            {
                ClientId = CognitoClientId,
                Password = regReq.Password,
                Username = regReq.UserName
            };

            var emailAttribute = new AttributeType
            {
                Name  = "email",
                Value = regReq.EMailAddress
            };

            signUpRequest.UserAttributes.Add(emailAttribute);
            // Add custom attributes
            signUpRequest.UserAttributes.Add(
                new AttributeType
            {
                Name  = CompanyNameAttribute,
                Value = regReq.CompanyName
            });

            return(await Cognito.SignUpAsync(signUpRequest));
        }
        /// <summary>
        /// Add custom attributes if not yet setup
        /// </summary>
        private async Task InitializeCustomAttributes()
        {
            var userPoolRequest = new DescribeUserPoolRequest
            {
                UserPoolId = CognitoUserPoolId
            };

            DescribeUserPoolResponse userPoolInfo = await Cognito.DescribeUserPoolAsync(userPoolRequest);

            var
                customAttributes = new List <SchemaAttributeType>();

            var companyAttr = new SchemaAttributeType
            {
                AttributeDataType = AttributeDataType.String,
                Name = CompanyNameAttribute
            };

            List <SchemaAttributeType> userPoolSchemaAttributes = userPoolInfo.UserPool.SchemaAttributes;

            if (userPoolSchemaAttributes.All(a => a.Name != companyAttr.Name))
            {
                customAttributes.Add(companyAttr);
            }
            if (customAttributes.Count > 0)
            {
                _logger.LogInformation($"Adding custom Cognito Attributes {CompanyNameAttribute}");
                await Cognito.AddCustomAttributesAsync(
                    new AddCustomAttributesRequest
                {
                    CustomAttributes = customAttributes,
                    UserPoolId       = CognitoUserPoolId
                });
            }
        }
 public CognitoUserProvider(
     Cognito cognito,
     AmazonCognitoIdentityProviderClient provider)
 {
     _cognito  = cognito;
     _provider = provider;
 }
        /// <summary>
        /// Add custom attributes if not yet setup
        /// </summary>
        private async Task InitializeCustomAttributes()
        {
            var userPoolRequest = new DescribeUserPoolRequest()
            {
                UserPoolId = CognitoUserPoolId
            };

            var userPoolInfo = await Cognito.DescribeUserPoolAsync(userPoolRequest);

            List <SchemaAttributeType>
            customAttributes = new List <SchemaAttributeType>();

            var companyAttr = new SchemaAttributeType()
            {
                AttributeDataType = AttributeDataType.String,
                Name = CompanyNameAttribute
            };

            List <SchemaAttributeType> userPoolSchemaAttributes = userPoolInfo.UserPool.SchemaAttributes;

            if (userPoolSchemaAttributes.All(a => a.Name != companyAttr.Name))
            {
                customAttributes.Add(companyAttr);
            }
            if (customAttributes.Count > 0)
            {
                await Cognito.AddCustomAttributesAsync(
                    new AddCustomAttributesRequest()
                {
                    CustomAttributes = customAttributes,
                    UserPoolId       = CognitoUserPoolId
                });
            }
        }
 public AppSettingsController(Cognito configuration)
 {
     _configuration = configuration;
 }
        /// <summary>
        /// Start signup process for new user in Cognito user pool
        /// </summary>
        /// <param name="regReq"></param>
        /// <returns></returns>
        public async Task <RegistrationPostResponse> RegisterUserAsync(RegistrationRequest regReq)
        {
            // Register the user using Cognito
            var signUpRequest = new SignUpRequest
            {
                ClientId = CognitoClientId,
                Password = regReq.Password,
                Username = regReq.UserName,
            };

            var emailAttribute = new AttributeType
            {
                Name  = "email",
                Value = regReq.EMailAddress
            };

            signUpRequest.UserAttributes.Add(emailAttribute);
            // Add custom attributes
            signUpRequest.UserAttributes.Add(new AttributeType
            {
                Name  = CompanyNameAttribute,
                Value = regReq.CompanyName
            });

            RegistrationPostResponse regPostResponse = new RegistrationPostResponse();

            try {
                var getResponse = await Cognito.SignUpAsync(signUpRequest);

                regPostResponse.success       = true;
                regPostResponse.statusCode    = getResponse.HttpStatusCode.ToString();
                regPostResponse.userSub       = getResponse.UserSub;
                regPostResponse.userConfirmed = getResponse.UserConfirmed;
            }
            catch (AmazonCognitoIdentityProviderException ace) {
                regPostResponse.error.message = ace.Message;
                regPostResponse.success       = false;
                switch (ace.ErrorCode)
                {
                case "UsernameExistsException":
                    regPostResponse.error.error = ErrorEnum.UserAlreadyExists;
                    break;

                case "email already exists":                          // not sure what this exception is
                    regPostResponse.error.error = ErrorEnum.UserAlreadyExists;
                    break;

                default:
                    regPostResponse.error.error = ErrorEnum.Unknown;
                    break;
                }
                regPostResponse.error.message = ace.ErrorCode + ":" + ace.Message;
            }
            catch (Exception e) {
                regPostResponse.success       = false;
                regPostResponse.error.error   = ErrorEnum.Unknown;
                regPostResponse.error.message = e.Message;
            }

            return(regPostResponse);
        }
 public HandlerModule(Cognito config, IHostingEnvironment env)
 {
     _config = config;
     _env    = env;
 }