Exemplo n.º 1
0
        public async Task <Response <UserIdentityDto> > ManageUserClaimAsync(ManageUserClaimDto manageUserClaimDto)
        {
            var userById = await _userManager.GetUserByIdAsync(manageUserClaimDto.UserId);

            var userByName = await _userManager.GetUserByNameAsync(manageUserClaimDto.UserName);

            if (userById != userByName)
            {
                return(Response <UserIdentityDto> .Fail("Forbidden"));
            }
            var allClaims = await _userManager.GetClaimsAsync(userById);

            var claimExists =
                allClaims.Where(x => x.Type == manageUserClaimDto.Type && x.Value == manageUserClaimDto.Value).ToList();

            switch (manageUserClaimDto.Checked)
            {
            case true when claimExists.Count == 0:
            {
                await _userManager.AddClaimAsync(userById,
                                                 new Claim(manageUserClaimDto.Type, manageUserClaimDto.Value));

                break;
            }

            case false when claimExists.Count > 0:
            {
                await _userManager.RemoveClaimsAsync(userById, claimExists);

                break;
            }
            }
            return(Response <UserIdentityDto> .Success(new UserIdentityDto { Id = manageUserClaimDto.UserId },
                                                       "Succeeded"));;
        }
Exemplo n.º 2
0
        private async Task <IdentityResult> SaveClaim()
        {
            var userId = Guid.Parse(lblUserId.Text);

            var newClaim = new Claim(txtClaimType.Text.Trim(), txtClaimName.Text.Trim());



            return(await _userManager.AddClaimAsync(userId, newClaim));
        }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
0
        public async Task <ActionResult> CreateAfterSend(Guid?id, ClaimsCreateViewModel model)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                var claim = new Claim(model.Type, model.Value);

                var result = await _userManager.AddClaimAsync(id.Value, claim);

                if (result.Succeeded)
                {
                    return(RedirectToActionPermanent("Index", new { id = id.Value }));
                }
            }

            return(View());
        }