Exemplo n.º 1
0
        public virtual async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                CustomUser customUser = model;
                customUser.TwoFactorEnabled = true;

                var user = await CustomUser.FindByNameAsync(model.Email);

                if (user != null && !user.PersonRoles.Any(r => r.RoleId == model.RoleID))
                {
                    //await CustomUser.AddToRoleAsync(user.Id, Enum.GetName(typeof(DTO.Constants.RolesEnum), model.RoleID));
                    //await CustomUser.AddClaimAsync(user.Id, new Claim(ClaimTypes.Role, model.RoleID.ToString()));

                    int            existingRoleID  = user.PersonRoles.Select(r => r.RoleId).First();
                    var            existedRoleName = existingRoleID == ConstantsDtos.Roles.Customer ? "RF Customer" : "RF Driver";
                    var            RoleName        = model.RoleID == ConstantsDtos.Roles.Customer ? "RF Customer" : "RF Driver";
                    string         loginUrl        = Url.Action(Mvc.Account.Login());
                    IList <string> message         = new List <string>();
                    message.Add(string.Format("We found your email address in our system and you already created an account with us as <strong> {0} </strong>.<br> To create new role please do the following:<ul><ol>Log in to your account.</ol> <ol>Click on Profile link</ol><ol>Click on Add New Role</ol></ul> Click here to go to login page <a class='btn btn-info' href='{loginUrl}'>LogIn</a> ", existedRoleName));

                    TempData["Errors"] = message;
                }
                else
                {
                    var result = await CustomUser.CreateAsync(customUser, model.Password);

                    if (result.Errors.Count() > 0)
                    {
                        // TempData["RegistrationError"] = "We Found you email in out system. Please try to login using your email and password.";
                    }
                    else
                    {
                        var accountBL = ServiceLocator.Current.GetInstance <IAccountBL>();

                        string emailCode = await _CustomUserManager.GenerateEmailConfirmationTokenAsync(customUser.Id);

                        string phoneCode = await CustomUser.GenerateTwoFactorTokenAsync(customUser.Id, "Phone Code");

                        await accountBL.SaveMobileGeneratedVerificationCode(customUser.ID, Convert.ToInt32(phoneCode));

                        // var accountBL = ServiceLocator.Current.GetInstance<IAccountBL>();
                        EmailTemplate emailTemplate = accountBL.GetEmail("EMAIL_VERIFICATION");

                        var    callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = customUser.Id, code = emailCode }, protocol: Request.Url.Scheme);
                        string emailBody   = String.Format(emailTemplate.Body, model.FirstName, callbackUrl);
                        await _CustomUserManager.SendEmailAsync(customUser.Id, emailTemplate.Subject, emailBody);

                        //await _CustomUserManager.SendSmsAsync(customUser.Id, "");

                        return(View(Mvc.Account.Views.DisplayEmail));
                    }

                    TempData["Errors"] = result.Errors;
                }
            }


            return(RedirectToAction(Mvc.Home.Index()));
        }
Exemplo n.º 2
0
        public virtual async Task <ActionResult> ResendCode()
        {
            var accountBL = ServiceLocator.Current.GetInstance <IAccountBL>();
            var userId    = await CustomSignIn.GetVerifiedUserIdAsync().WithCurrentCulture();

            string phoneCode = await CustomUser.GenerateTwoFactorTokenAsync(userId, "Phone Code");

            await accountBL.SaveMobileGeneratedVerificationCode(Convert.ToInt32(userId), Convert.ToInt32(phoneCode));

            return(Json(new { }));
        }