Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                DateTime BirthDateMiladi = convertDate.ShamsiToMiladi(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 = await roleManager.FindByNameAsync("کاربر");

                    if (role == null)
                    {
                        await roleManager.CreateAsync(new ApplicationRole("کاربر"));
                    }

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

                    await userManager.AddClaimAsync(user, new Claim(ClaimTypes.DateOfBirth, BirthDateMiladi.ToString("MM/dd")));

                    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());
        }
Ejemplo n.º 2
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.ShamsiToMiladi(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());
        }