public IHttpActionResult Authenticate(SignInRequest request)
        {
            var response = new SignInResponse();

            try
            {
                if (request.IsValidModel())
                {
                    var member = MembershipAdapter.GetMember(request.Email);

                    if (!member.Authenticate(request.Password))
                    {
                        request.AddError("Password", "Authentication failed");
                    }
                    else
                    {
                        response.MemberId = member.Id.ToString();
                        response.Alias = member.Profile.Alias;
                        response.IsActive = member.IsActive;
                        response.IsAdmin = member.IsAdmin;
                    }
                }

                if (!request.HasErrors())
                {
                    response.IsSuccessful = true;
                    response.StatusMessage = "Sign in succeeded";
                }
                else
                {
                    response.IsSuccessful = false;
                    response.StatusMessage = "Sign in was unsuccessful";
                    response.Errors.AddRange(request.GetErrors());
                }
            }
            catch (Exception ex)
            {
                request.Password = string.Empty;

                this.Log<SignInRequest>(LogCategories.Error, request, ex.Message);

                response.IsSuccessful = false;
                response.StatusMessage = this.StatusMessageForExpection;
                response.Errors.Add(ex.Message);
            }

            return Ok(response);
        }
        public IHttpActionResult ResetPasswordToken(ResetPasswordRequest request)
        {
            var response = new SignInResponse();

            try
            {
                var isValidRequest = false;

                var memberId = request.GetMemberId();
                var member = MembershipAdapter.GetMember(request.Token);

                if (request.IsValidModel())
                {
                    if (!request.PasswordsMatch())
                    {
                        request.AddError("Password", "Passwords do not match");
                    }
                    else
                    {
                        if (member.Exists())
                        {
                            if (memberId == member.Id)
                            {
                                if (member.HasTokens())
                                {
                                    if (member.Tokens.ElementAt(0).IsValid(TokenTypes.PasswordReset))
                                    {
                                        isValidRequest = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (isValidRequest)
                {
                    var memberPassword = new MemberPassword();
                    memberPassword.MemberId = member.Id;
                    memberPassword.InitializePassword(request.Password);

                    MembershipAdapter.UpdatePassword(memberPassword);

                    var memberToken = new MemberToken
                    {
                        Token = member.Tokens.ElementAt(0).Token,
                        MemberId = member.Id,
                        TokenType = member.Tokens.ElementAt(0).TokenType,
                        ExpirationDate = member.Tokens.ElementAt(0).ExpirationDate,
                        ConfirmedDate = DateTime.Now.ToUniversalTime(),
                        RequestDate = member.Tokens.ElementAt(0).RequestDate
                    };

                    MembershipAdapter.UpdateToken(memberToken);

                    response.MemberId = member.Id.ToString();
                    response.Alias = member.Profile.Alias;
                    response.IsActive = member.IsActive;
                    response.IsAdmin = member.IsAdmin;

                    var email = new ResetPasswordEmail
                    {
                        DomainName = ConfigHelper.DomainName,
                        DomainBaseUrl = ConfigHelper.DomainBaseUrl,
                        Alias = member.Profile.Alias
                    };

                    var emailBuilder = new EmailBuilder(EmailReference.HTML.Templates.Main, EmailReference.HTML.Contents.ResetPassword);
                    var emailBody = emailBuilder.BuildBody<ResetPasswordEmail>(email);
                    var emailResult = EmailHelper.SendEmail(ConfigHelper.NoReplayEmailAddress,
                                                            ConfigHelper.DomainName,
                                                            member.Email,
                                                            member.Profile.Alias,
                                                            email.Subject,
                                                            emailBody,
                                                            true);

                    if (emailResult)
                    {
                        response.IsSuccessful = true;
                        response.StatusMessage = "Reset password succeeded";
                    }
                }
                else
                {
                    response.IsSuccessful = false;
                    response.StatusMessage = "Reset password was unsuccessful";
                    response.Errors.AddRange(request.GetErrors());
                }
            }
            catch (Exception ex)
            {
                request.Password = string.Empty;
                request.PasswordConfirm = string.Empty;

                this.Log<ResetPasswordRequest>(LogCategories.Error, request, ex.Message);

                response.IsSuccessful = false;
                response.StatusMessage = this.StatusMessageForExpection;
                response.Errors.Add(ex.Message);
            }

            return Ok(response);
        }
        public IHttpActionResult ConfirmRegistrationToken(Guid token)
        {
            var response = new SignInResponse();

            try
            {
                var isValidToken = false;

                var member = MembershipAdapter.GetMember(token);

                if (member.Exists())
                {
                    if (member.HasTokens())
                    {
                        if (member.Tokens.ElementAt(0).IsValid(TokenTypes.Registration))
                        {
                            isValidToken = true;
                        }
                    }
                }

                if (isValidToken)
                {
                    member.MembershipDate = DateTime.Now.ToUniversalTime();
                    member.Tokens.ElementAt(0).ConfirmedDate = member.MembershipDate;

                    member = MembershipAdapter.ConfirmRegistration(member);

                    var email = new RegistrationConfirmationEmail
                    {
                        DomainName = ConfigHelper.DomainName,
                        DomainBaseUrl = ConfigHelper.DomainBaseUrl,
                        Alias = member.Profile.Alias
                    };

                    var emailBuilder = new EmailBuilder(EmailReference.HTML.Templates.Main, EmailReference.HTML.Contents.RegistrationConfirmation);
                    var emailBody = emailBuilder.BuildBody<RegistrationConfirmationEmail>(email);
                    var emailResult = EmailHelper.SendEmail(ConfigHelper.NoReplayEmailAddress,
                                                            ConfigHelper.DomainName,
                                                            member.Email,
                                                            member.Profile.Alias,
                                                            email.Subject,
                                                            emailBody,
                                                            true);
                    if (emailResult)
                    {
                        response.MemberId = member.Id.ToString();
                        response.Alias = member.Profile.Alias;
                        response.IsActive = member.IsActive;
                        response.IsAdmin = member.IsAdmin;

                        response.IsSuccessful = true;
                        response.StatusMessage = "Confirm registration succeeded";
                    }
                }
                else
                {
                    response.IsSuccessful = false;
                    response.StatusMessage = "Confirm registration was unsuccessful";
                }
            }
            catch (Exception ex)
            {
                this.Log(LogCategories.Error, token.ToString(), ex.Message);

                response.IsSuccessful = false;
                response.StatusMessage = this.StatusMessageForExpection;
                response.Errors.Add(ex.Message);
            }

            return Ok(response);
        }