public ActionResult SendChangePasswordLink(string Name, string Email, string RequestID, string SessionID)
        {
            if (AppSession.Parameters.RegistrationResetPassword.Value == "false")
            {
                if (AppSession.IsMemberInAdminRole)
                {

                }
                else
                {
                    string ReturnUrl = AppSession.ReturnUrl;
                    ReturnUrl = (ReturnUrl != null && ReturnUrl.Length > 0) ? ReturnUrl : ((AppSession.Parameters.RulesRedirectAfterLogin.Value != null && AppSession.Parameters.RulesRedirectAfterLogin.Value.Length > 0) ? AppSession.Parameters.RulesRedirectAfterLogin.Value : Url.Content("~/"));

                    AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "Somebody tries to request changing password but it was switched off. ", AuditEvent.GetSessionDetails());
                    return new RedirectResult(ReturnUrl);
                }
            }

            RequestResultModel _model = new RequestResultModel();

            if (Name == null || Name.Trim().Length == 0 ||
               Email == null || Email.Trim().Length == 0 ||
               !AppSession.IsEmail(Email))
            {
                _model = new RequestResultModel();
                _model.Title = "Warning";
                _model.InfoType = RequestResultInfoType.ErrorOrDanger;

                if (Name == null || Name.Trim().Length == 0)
                    _model.Message = String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.NameIsEmpty"));

                if (Email == null || Email.Trim().Length == 0)
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.EmailIsEmpty"));

                if (Email != null && !AppSession.IsEmail(Email))
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.EmailWrongFromat"));

                _model.Message = String.Format("<ul>{0}</ul>", _model.Message);

                return Json(new
                {
                    Status = RequestResultInfoType.ErrorOrDanger,
                    NotifyType = NotifyType.DialogInline,
                    Html = this.RenderPartialView(@"_RequestResultDialogInLine", _model)

                }, JsonRequestBehavior.AllowGet);

            }

               Member Member = Members.GetByEmailName(Email, Name);

               if (!AppSession.IsEmail(Email))
               {
               _model.Title = GetLabel("Account.Controller.Warning");
               _model.Message = GetLabel("Account.Controller.WrongEmail");
               _model.InfoType = RequestResultInfoType.ErrorOrDanger;
               AuditEvent.AppEventWarning(AppSession.Parameters.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountWrongEmail, Member.Name, Member.Email));
               }
               else
               {
               if (Member.MemberID <= 0)
               {
                   _model.Title = GetLabel("Account.Controller.Warning");
                   _model.Message = GetLabel("Account.Controller.AccountDoesntExist");
                   _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                   AuditEvent.AppEventWarning(AppSession.Parameters.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountAccountDoesntExist, Name, Email));
               }
               else
               {
                   try
                   {
                       string ResetToken = Web.Admin.Logic.StringTool.RandomString(20);

                       if (!SendChangePasswordEmail(Name, Email, ResetToken))
                       {
                           throw new Exception("Changing password link notification e-mail has not been send!");
                       };

                       _model.Title = GetLabel("Account.Controller.Congrat");
                       _model.Message = GetLabel("Account.Controller.ResetPassMail");
                       _model.InfoType = RequestResultInfoType.Success;

                       ActionToken actionToken = new ActionToken();
                       actionToken.Email = Email;
                       actionToken.Token = HttpUtility.UrlDecode(ResetToken);
                       actionToken.ActionType = ActionTokenTypes.PasswordReset;
                       actionToken.ReturnURL = AppSession.ReturnUrl != null ? AppSession.ReturnUrl : (AppSession.Parameters.RulesRedirectAfterLogout.Value != "" ? AppSession.Parameters.RulesRedirectAfterLogout.Value : "");
                       actionToken.Save();

                       AuditEvent.AppEventSuccess(AppSession.Parameters.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountResetPassMail, Member.Name, Member.Email));

                   }
                   catch (Exception ex) {
                       _model.Title = GetLabel("Account.Controller.Warning");
                       _model.Message = GetLabel("Account.Controller.CantSendNotification");
                       _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                       AuditEvent.AppEventError(Email ,"Send change password link: Something went wrong with e-mail notification: " + ex.Message, AuditEvent.GetSessionDetails(ex.StackTrace),true);
                    }
               }
               }

               return Json(new
               {
               Status = _model.InfoType,
               NotifyType = NotifyType.DialogInline,
               Html = this.RenderPartialView(@"_RequestResultDialogInLine", _model)

               }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Register(string Name, string Email, string Password, string Confirmation, string ReturnUrl, List<ProfileFieldInfo> FieldInfo, string RequestID, string SessionID, int TimeZoneOffset)
        {
            AppParams.RefreshAppParameters();
            string AfterSingUpRedirectUrl = "";

            if (AppSession.Parameters.RegistrationSelfRegistration.Value == "false")
            {
                if (AppSession.IsMemberInAdminRole)
                {

                }
                else
                {

                    if (ReturnUrl == null || ReturnUrl.Trim().Length == 0)
                        ReturnUrl = (ReturnUrl != null && ReturnUrl.Length > 0) ? ReturnUrl : ((AppSession.Parameters.RulesRedirectAfterLogin.Value != null && AppSession.Parameters.RulesRedirectAfterLogin.Value.Length > 0) ? AppSession.Parameters.RulesRedirectAfterLogin.Value : Url.Content("~/"));

                    AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "Somebody tries to Register notification but it was switched off. ", AuditEvent.GetSessionDetails());
                    return new RedirectResult(ReturnUrl);
                }
            }

            RequestResultModel _model = new RequestResultModel();

            if (AppSession.IsEmail(Name))
            {
                _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.NameLooksLikeEmail"));
            }

            if (Name == null || Name.Trim().Length == 0 ||
               ((Password == null || Password.Trim().Length == 0)) ||
               Email == null || Email.Trim().Length == 0 ||
               Password != Confirmation ||
               !AppSession.IsEmail(Email) ||
               (Password != null && Password.Length > 0 && Password.Length <=3) ||
               (Confirmation != null && Confirmation.Length > 0 && Confirmation.Length <=3))
            {
                if (Name == null || Name.Trim().Length == 0)
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.NameIsEmpty"));

                if ((Password == null || Password.Trim().Length == 0))
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.PasswordIsEmty"));

                if (Email == null || Email.Trim().Length == 0)
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.EmailIsEmpty"));

                if (Email != null && !AppSession.IsEmail(Email))
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.EmailWrongFromat"));

                if (Password != Confirmation)
                    _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.PasswordNoMatch"));

                if ((Password != null && Password.Length > 0 && Password.Length < AppSession.PasswordMaxLength) ||
                (Confirmation != null && Confirmation.Length > 0 && Confirmation.Length < AppSession.PasswordMaxLength))
                {
                    _model.Message += String.Format("<li>{0}</<li>", String.Format(GetLabel("Account.CreateAccount.PasswordLength"), AppSession.PasswordMaxLength));
                }
            }

            // Check mandatory fields
            if (FieldInfo != null)
            {
                foreach (ProfileFieldInfo Field in FieldInfo)
                {
                    ProfileField profileField = ProfileFields.GetBy(int.Parse(Field.ID));
                    if (profileField.IsMandatory == 1 && (Field.Value == null || Field.Value.Trim().Length == 0))
                    {
                        _model.Message += String.Format("<li>{0}</li>", String.Format(GetLabel("Account.CreateAccount.Mandatory"), profileField.FieldName));
                    }

                    if (profileField.IsMandatory == 1 && profileField.FieldTypeID == ProfileFieldTypeEnum.CheckBox && Field.Value != null && Field.Value.ToLower() == "false")
                    {
                        _model.Message += String.Format("<li>{0}</li>", String.Format(GetLabel("Account.CreateAccount.Mandatory"), profileField.FieldName));
                    }
                }
            }

            bool nonAlphaNumeric = Name.Replace(" ", "").All(c => char.IsLetterOrDigit(c));
            if (!nonAlphaNumeric)
            {
                _model.Message += String.Format("<li>{0}</<li>", GetLabel("Account.CreateAccount.NameNonAlfaNum"));
            }

            if (_model.Message.Length > 0)
            {
                _model.Message = String.Format("<ul class=\"error-message-list\">{0}</ul>", _model.Message);
                _model.InfoType = RequestResultInfoType.ErrorOrDanger;

                return Json(new
                {
                    Status = RequestResultInfoType.ErrorOrDanger,
                    NotifyType = NotifyType.DialogInline,
                    Html = this.RenderPartialView(@"_RequestResultDialogInLine", _model)

                }, JsonRequestBehavior.AllowGet);
            }

            // Check email format
            if (!AppSession.IsEmail(Email))
            {
                _model.Title = GetLabel("Account.Controller.Warning");
                _model.Message = GetLabel("Account.Controller.WrongEmail");
                _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                AuditEvent.AppEventError(AppParams.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountWrongEmail, Name, Email));
            }
            else
            {
                if (!AppSession.IsMailDomainAllowed(Email))
                {
                    _model.Title = GetLabel("Account.Controller.Warning");
                    _model.Message = GetLabel("Account.Controller.DomainNotAllowed");
                    _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                    AuditEvent.AppEventError(AppParams.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountDomainNotAllowed, Name, Email));
                }
                else
                {
                    Member Member = Members.GetByEmailOrName(Email, Name);

                    if (Member.MemberID > 0)
                    {
                        _model.Title = GetLabel("Account.Controller.Warning");
                        _model.Message = GetLabel("Account.Controller.AccountExists");
                        _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                        AuditEvent.AppEventError(AppParams.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountAccountExists, Name, Email));
                    }
                    else
                    {
                        // Was account activated ?
                        // Was account blocked ?

                        Member member = new Logic.Objects.Member();
                        member.Name = Name;
                        member.Email = Email;
                        member.Password = Password;
                        member.IsBuiltIn = false;
                        member.Created = DateTime.UtcNow;

                        member.Save();

                        try
                        {
                            string ActivateToken = Web.Admin.Logic.StringTool.RandomString(20);

                            if (!SendActivationEmail(Name, Email, ActivateToken)){
                                throw new Exception("Creating an account notification e-mail has not been send!");
                            }

                            ActionToken actionToken = new ActionToken();
                            actionToken.Email = Email;
                            actionToken.Token = HttpUtility.UrlDecode(ActivateToken);
                            actionToken.ActionType = ActionTokenTypes.SelfActivation;
                            actionToken.ReturnURL = AppSession.ReturnUrl != null ? AppSession.ReturnUrl : ( AppSession.Parameters.RulesRedirectAfterLogout.Value != "" ? AppSession.Parameters.RulesRedirectAfterLogout.Value : "") ;
                            actionToken.Save();

                            if (member.MemberID > 0)
                            {
                                string[] RoleNames = AppParams.RegistrationMemberRegistrationToRoles.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (String RoleName in RoleNames)
                                {
                                    Role role = Web.Admin.Logic.Collections.Roles.GetBy(RoleName.Trim());
                                    if (role.RoleID > 0)
                                    {
                                        MemberRole memberrole = new MemberRole();
                                        memberrole.MemberID = member.MemberID;
                                        memberrole.RoleID = role.RoleID;
                                        memberrole.Save();
                                    }
                                }
                            }

                            if (FieldInfo != null)
                            {
                                foreach (ProfileFieldInfo Field in FieldInfo)
                                {
                                    ProfileField profileField = ProfileFields.GetBy(int.Parse(Field.ID));

                                    MemberProfileField memberProfileField = new MemberProfileField();
                                    memberProfileField.MemberID = member.MemberID;
                                    memberProfileField.FieldID = int.Parse(Field.ID);
                                    memberProfileField.FieldValue = Field.Value;

                                    if (Field.Value == null || Field.Value.Trim().Length == 0)
                                        memberProfileField.Delete();
                                    else
                                        memberProfileField.Save();
                                }
                            }

                             Domain _domain = Domains.GetByName(AppSession.SignUpDomain);

                             if (_domain.DomainID > 0)
                              {
                                  MemberDomain _memberDomain = new MemberDomain();
                                  _memberDomain.DomainID = _domain.DomainID;
                                  _memberDomain.MemberID = member.MemberID;
                                  _memberDomain.Save();
                              }

                            if (AppSession.Parameters.RulesRedirectAfterSignUp.Value != null && AppSession.Parameters.RulesRedirectAfterSignUp.Value.Length > 0)
                                AfterSingUpRedirectUrl = AppSession.Parameters.RulesRedirectAfterSignUp.Value;

                            _model.Title = GetLabel("Account.Controller.Congrat");
                            _model.Message = GetLabel("Account.Controller.ActivationMailSent");
                            _model.InfoType = RequestResultInfoType.Success;
                            AuditEvent.AppEventSuccess(AppParams.GeneralAdminEmail.Value, String.Format(AuditEvent.AccountActivationMailSent, Name, Email));

                        }
                        catch (Exception ex) {
                            try
                            {
                                // Try to delete member if notification was failed.
                                member.Delete();
                            }
                            catch { }

                            _model.Title = GetLabel("Account.Controller.Warning");
                            _model.Message = GetLabel("Account.Controller.CantSendNotification");
                            _model.InfoType = RequestResultInfoType.ErrorOrDanger;
                            AuditEvent.AppEventError(Email ,"Registration: Something went wrong with e-mail notification: " + ex.Message, AuditEvent.GetSessionDetails(ex.StackTrace),true);
                        }
                    }
                }
            }

            return Json(new
               {
               Status = _model.InfoType,
               RedirectTo = AfterSingUpRedirectUrl,
               NotifyType = NotifyType.DialogInline,
               Html = this.RenderPartialView(@"_RequestResultDialogInLine", _model)

               }, JsonRequestBehavior.AllowGet);
        }