Beispiel #1
0
        public async Task <IActionResult> Register(RegisterViewModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                DateTime BirthDateMiladi = _convertDate.ConvertShamsiToMiladi(ViewModel.BirthDate);
                var      user            = new ApplicationUser {
                    UserName = ViewModel.UserName, Email = ViewModel.Email, PhoneNumber = ViewModel.PhoneNumber, RegisterDate = DateTime.Now, IsActive = true, BirthDate = BirthDateMiladi
                };
                IdentityResult result = await _userManager.CreateAsync(user, ViewModel.Password);

                if (result.Succeeded)
                {
                    var role = _roleManager.FindByNameAsync("کاربر");
                    if (role == null)
                    {
                        await _roleManager.CreateAsync(new ApplicationRole("کاربر"));
                    }

                    result = await _userManager.AddToRoleAsync(user, "کاربر");

                    await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.DateOfBirth, BirthDateMiladi.ToShortDateString()));

                    if (result.Succeeded)
                    {
                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", values: new { userId = user.Id, code = code }, protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(ViewModel.Email, "تایید ایمیل حساب کاربری - سایت میزفا", $"<div dir='rtl' style='font-family:tahoma;font-size:14px'>لطفا با کلیک روی لینک رویه رو ایمیل خود را تایید کنید.  <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>کلیک کنید</a></div>");

                        return(RedirectToAction("Index", "Home", new { id = "ConfirmEmail" }));
                    }
                }

                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, item.Description);
                }
            }

            return(View());
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/Admin/UsersManager/Index?Msg=Success");
            if (ModelState.IsValid)
            {
                ConvertDate convertDate = new ConvertDate();
                var         user        = new ApplicationUser {
                    UserName = Input.UserName, Email = Input.Email, FirstName = Input.Name, LastName = Input.Family, PhoneNumber = Input.PhoneNumber, BirthDate = convertDate.ConvertShamsiToMiladi(Input.BirthDate), IsActive = true, RegisterDate = DateTime.Now, EmailConfirmed = true, TwoFactorEnabled = Input.TwoFactorEnable
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    if (UserRoles != null)
                    {
                        await _userManager.AddToRolesAsync(user, UserRoles);
                    }

                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { userId = user.Id, code = code },
                    //    protocol: Request.Scheme);

                    //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            GetRoles = _roleManager.GetAllRoles();
            // If we got this far, something failed, redisplay form
            return(Page());
        }
Beispiel #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/Admin/UsersManager/Index?Msg=Success");
            if (ModelState.IsValid)
            {
                ConvertDate convert = new ConvertDate();
                var         user    = new ApplicationUser {
                    UserName = Input.UserName, Email = Input.Email, FirstName = Input.Name, LastName = Input.Family, PhoneNumber = Input.PhoneNumber, BirthDate = convert.ConvertShamsiToMiladi(Input.BirthDate), IsActive = true, RegisterDate = DateTime.Now, EmailConfirmed = true, TwoFactorEnabled = Input.TwoFactorEnabled
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    if (UserRoles != null)
                    {
                        await _userManager.AddToRolesAsync(user, UserRoles);
                    }

                    return(LocalRedirect(returnUrl));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            GetRoles = _roleManager.GetAllRoles();
            return(Page());
        }