Beispiel #1
0
        /// <summary>
        /// Create a new Customer
        /// </summary>
        /// <param name="createUserParam">The Repository call params <see cref="CreateUserParam"/></param>
        /// <returns>
        /// The created Customer
        /// </returns>
        public virtual async Task <Customer> CreateUserAsync(CreateUserParam createUserParam)
        {
            if (createUserParam == null)
            {
                throw new ArgumentNullException(nameof(createUserParam));
            }
            if (createUserParam.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(createUserParam.CultureInfo)), nameof(createUserParam));
            }
            if (string.IsNullOrWhiteSpace(createUserParam.Password))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(createUserParam.Password)), nameof(createUserParam));
            }
            if (string.IsNullOrWhiteSpace(createUserParam.Email))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(createUserParam.Email)), nameof(createUserParam));
            }
            if (string.IsNullOrWhiteSpace(createUserParam.FirstName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(createUserParam.FirstName)), nameof(createUserParam));
            }
            if (string.IsNullOrWhiteSpace(createUserParam.LastName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(createUserParam.LastName)), nameof(createUserParam));
            }
            if (string.IsNullOrWhiteSpace(createUserParam.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(createUserParam.Scope)), nameof(createUserParam));
            }

            var request = new CreateCustomerMembershipRequest
            {
                Id               = createUserParam.CustomerId,
                Username         = createUserParam.Username,
                Email            = createUserParam.Email,
                FirstName        = createUserParam.FirstName,
                LastName         = createUserParam.LastName,
                Password         = createUserParam.Password,
                PasswordQuestion = createUserParam.PasswordQuestion,
                PasswordAnswer   = createUserParam.PasswordAnswer,
                Language         = createUserParam.CultureInfo.Name,
                ScopeId          = createUserParam.Scope
            };

            var createdCustomer = await OvertureClient.SendAsync(request).ConfigureAwait(false);

            return(createdCustomer);
        }
Beispiel #2
0
        /// <summary>
        /// Adds a new membership user to the data source.
        /// </summary>
        /// <param name="username">The user name could be user's email depending on configuration.</param>
        /// <param name="password">The password for the new user.</param>
        /// <param name="email">The e-mail address for the new user.</param>
        /// <param name="passwordQuestion">The password question for the new user.</param>
        /// <param name="passwordAnswer">The password answer for the new user</param>
        /// <param name="isApproved">The new user is always approved no matter the value of this parameter.</param>
        /// <param name="providerUserKey">The unique identifier from the membership data source for the user. Generated if null.</param>
        /// <param name="status">A <see cref="T:System.Web.Security.MembershipCreateStatus"/> enumeration value indicating whether the user was created successfully.</param>
        /// <returns>
        /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the information for the newly created user.
        /// </returns>
        public override MembershipUser CreateUser(string username, string password, string email,
                                                  string passwordQuestion, string passwordAnswer,
                                                  bool isApproved, object providerUserKey,
                                                  out MembershipCreateStatus status)
        {
            string domainUsername;

            if (TryGetDomainUser(username, out domainUsername))
            {
                // Username shouldn't contain domain
                status = MembershipCreateStatus.UserRejected;
                return(null);
            }

            var args = new ValidatePasswordEventArgs(domainUsername, password, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }

            Guid userId = Guid.Empty;

            if (providerUserKey != null)
            {
                if (!(providerUserKey is Guid))
                {
                    status = MembershipCreateStatus.InvalidProviderUserKey;
                    return(null);
                }

                userId = (Guid)providerUserKey;
            }

            var request = new CreateCustomerMembershipRequest
            {
                Id               = userId,
                Username         = username,
                Email            = email,
                FirstName        = OvertureMembershipConfiguration.DefaultFirstName, // Required in Overture
                LastName         = OvertureMembershipConfiguration.DefaultLastName,  // Required in Overture
                Password         = password,
                PasswordQuestion = passwordQuestion,
                PasswordAnswer   = passwordAnswer,
                ScopeId          = GetCurrentScope(),
            };

            try
            {
                var createdCustomer = _client.Send(request);

                status = MembershipCreateStatus.Success;

                return(ConvertToMembershipUser(createdCustomer));
            }
            catch (ValidationError ex)
            {
                switch (ex.ErrorCode)
                {
                case "InvalidPassword":
                case "InvalidOperationException":
                    status = MembershipCreateStatus.InvalidPassword;
                    return(null);

                case "PasswordQuestionNoSet":
                    status = MembershipCreateStatus.InvalidQuestion;
                    return(null);

                case "UserNameAlreadyUsed":
                    status = MembershipCreateStatus.DuplicateUserName;
                    return(null);

                default:
                    status = MembershipCreateStatus.UserRejected;
                    return(null);
                }
            }
            catch (WebException ex)
            {
                throw new MembershipCreateUserException(ex.Message, ex);
            }
            catch (WebServiceException ex)
            {
                throw new MembershipCreateUserException(ex.ErrorMessage, ex);
            }
        }