public RequestsDto CreateRequest(CreateRequestsInput input)
 {
     RequestsDto results = new RequestsDto();
     return (RequestsDto)_creditsHeroConnect.CallCreditsHeroService<RequestsDto>(results, input,
         "api/services/app/Requests/CreateRequests");
 }
        public virtual async Task<ActionResult> SendRequest(FormCollection collection, LoginFormViewModel inquiryModel,
            string returnUrl = "")
        {
            string generatedPassword = System.Web.Security.Membership.GeneratePassword(8, 2);

            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                returnUrl = Request.ApplicationPath;
            }

            LoginFormViewModel loginFormModel = new LoginFormViewModel()
            {
                InquiryResults = new CreditsHero.Messaging.Requests.Dtos.GetInquiryResults(),
                RequestResults = new CreditsHero.Messaging.Dtos.RequestsDto(),
                ReturnUrl = returnUrl,
                IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled
            };

            #region Request
            CreditsHero.Messaging.Dtos.CreateRequestsInput requestInput = new CreditsHero.Messaging.Dtos.CreateRequestsInput();
            CreditsHero.Messaging.Dtos.RequestsDto requestResults;
            List<KeyValuePair<string, string>> inquiryValues = new List<KeyValuePair<string, string>>();

            BuildInquiry(ref inquiryValues);

            //Serialize object to JSON
            MemoryStream jsonStream = new MemoryStream();

            requestInput.QueryRequest = inquiryValues;
            requestInput.CompanyId = Guid.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["creditsHero:CompanyId"]);
            requestInput.FullName = String.Format("{0} {1}", Request.Form["FirstName"], Request.Form["LastName"]);
            requestInput.InquiryId = Int32.Parse(Request.Form["InquiryId"]);
            requestInput.SmsNumber = Request.Form["PhoneNumber"];
            requestInput.RequestsId = 0;
            requestInput.Email = Request.Form["EmailAddress"];
            requestInput.Comment = Request.Form["Comments"];
            requestInput.ReplyToEmail = System.Web.Configuration.WebConfigurationManager.AppSettings["creditsHero:EmailReply"];

            requestResults = _memberAppService.CreateRequest(requestInput);
            #endregion

            #region Request Ext
            CreateRequestsExtInput input = new CreateRequestsExtInput();
            input.KeyValues = new System.Collections.Hashtable();
            input.RequestsId = requestResults.Id;
            input.KeyValues.Add("Budget", Request.Form["Budget"]);
            input.KeyValues.Add("City", Request.Form["City"]);
            input.KeyValues.Add("State", Request.Form["StateValues"]);

            _memberAppService.CreateRequestExt(input);
            #endregion

            SetupLoginModel(ref loginFormModel, inquiryModel.InquiryResults, requestResults, string.Empty);

            #region User
            try
            {
                User user;

                //See if user exists (based on email address)
                user = _userManager.FindByEmail(requestInput.Email);

                if (user == null)
                {
                    var fullName = requestInput.FullName.Split(' ');
                    //Create user
                    user = new User
                    {
                        TenantId = null,
                        Name = fullName[0],
                        Surname = fullName[fullName.Length-1],
                        EmailAddress = requestInput.Email,
                        IsActive = true,
                        UserName = requestInput.Email,
                        Password = new PasswordHasher().HashPassword(generatedPassword),
                        IsEmailConfirmed = false,
                        IsDeleted = false,
                        CreationTime = DateTime.Now
                    };

                    //Switch to the tenant
                    _unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MayHaveTenant);
                    _unitOfWorkManager.Current.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, null);

                    //Add default roles
                    user.Roles = new List<UserRole>();
                    foreach (var customerRole in _roleManager.Roles.Where(r => r.Name == "Customer").ToList())
                    {
                        user.Roles.Add(new UserRole { RoleId = customerRole.Id });
                    }

                    //Save user
                    var result = _userManager.Create(user);

                    //Send email to user with password
                    SendEmail(
                        new CreditsHero.Messaging.Requests.Dtos.GetInquiryInput()
                        {
                            CompanyId = Guid.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["creditsHero:CompanyId"]),
                            QueryRequest = inquiryValues
                        },
                        System.Web.Configuration.WebConfigurationManager.AppSettings["creditsHero:EmailReply"],
                        "Welcome to Roofer Locator",
                        user.EmailAddress,
                        String.Format("<!DOCTYPE html><html lang=en xmlns='http://www.w3.org/1999/xhtml'><head><meta charset=utf-8 /><title></title><link rel='stylesheet' href='/Content/bootstrap-cosmo.min.css' /></head><body style='background-color:gainsboro;text-align:center;padding:0px;margin:0px'><div class='row' style='padding:0px;margin:0px;'> <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2'></div><div class='col-lg-8 col-md-8 col-sm-8 col-xs-8' style='padding:0px;margin:0px'> <div class='row'> <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2'></div><div class='col-lg-8 col-md-8 col-sm-8 col-xs-8' style='box-shadow: 5px 0px 5px -4px rgba(31,73,125,0.8), -5px 0px 8px -5px rgba(31,73,125,0.8); background-image: -ms-linear-gradient(rgb(25, 68, 125) 20%, rgb(32, 128, 190) 100%);'> <img src='{0}/images/rooferlocatorlogo_70x170.png' /> <div class='row' style='padding:0px;margin:0px;height:50px;font-family:Arial;font-size:24pt'> <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12' style='border:2px solid white;border-radius:5px; background:white;'> <div class='row' style='padding-left:13px;padding-right:13px;border-radius:5px;'> <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12' style='background:#fe7900;'>Welcome</div></div><div class='row' style='color:black'> <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12' style='font-size:16px;'>Welcome to RooferLocator.com! <p/><div style='font-size:22px;'> The following are your credentials<p/> Username = {1} <br/> Password = {2}</div></div></div><div class='row' style='padding-left:13px;padding-right:13px;border-radius:5px;'> <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12' style='background:#fe7900;'></div></div><div class='col-lg-12 col-md-12 col-sm-12 col-xs-12' style='border-top:1px solid gainsboro;padding:10px;color:black'> <div></div></div></div></div></div><div class='col-lg-2 col-md-2 col-sm-2 col-xs-2'></div></div></div><div class='col-lg-2 col-md-2 col-sm-2 col-xs-2' style='padding:0px;margin:0px'></div></div></body></html>"
                        , System.Web.Configuration.WebConfigurationManager.AppSettings["dashboardHero:DashboardPrefix"], user.EmailAddress, generatedPassword));

                    AbpUserManager<Tenant, Role, User>.AbpLoginResult loginResult;
                    loginResult = await GetLoginResultAsync(user.UserName, generatedPassword, null);

                    if (loginResult.Result == AbpLoginResultType.Success)
                    {
                        await SignInAsync(loginResult.User, loginResult.Identity);
                        return Redirect(Url.Action("Index", "Customers"));
                    }
                }

                //Directly login if possible
                if (user.IsActive)
                {
                    AbpUserManager<Tenant, Role, User>.AbpLoginResult loginResult;
                    loginResult = await GetLoginResultAsync(user.UserName, user.Password, null);
                    
                    if (loginResult.Result == AbpLoginResultType.Success)
                    {
                        await SignInAsync(loginResult.User, loginResult.Identity);
                        //return Redirect(Url.Action("Index", "Home"));
                        //return Redirect(Url.Action("RegisterMember", "Register"));
                        return Redirect(Url.Action("Index", "Customers"));
                    }

                    Logger.Warn("New registered user could not be login. This should not be normally. login result: " + loginResult.Result);
                }

                //If can not login, show a register result page
                return View("CustomerRequestResult", new CreditsHero.Messaging.Dtos.RequestsDto
                {
                    Email = user.EmailAddress,
                    FullName = user.Name + " " + user.Surname,
                    Id = Int32.Parse(user.Id.ToString())
                });
            }
            catch (UserFriendlyException ex)
            {
                ViewBag.IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled;
                ViewBag.ErrorMessage = ex.Message;

                //If can not login, show a register result page
                return View("CustomerRequestResult", new CreditsHero.Messaging.Dtos.RequestsDto());
            }
            #endregion
        }