Exemple #1
0
        public IActionResult Index()
        {
            ViewBag.Msg = "This is a test";

            ViewBag.EncryptedMsg1 = _protectionProviderService.Encrypt(ViewBag.Msg);
            ViewBag.DecryptMsg1   = _protectionProviderService.Decrypt(ViewBag.EncryptedMsg1);

            ViewBag.EncryptedMsg2 = _protectionProviderService.Encrypt(ViewBag.Msg);
            ViewBag.DecryptMsg2   = _protectionProviderService.Decrypt(ViewBag.EncryptedMsg2);

            return(View());
        }
Exemple #2
0
        /// <summary>
        /// EncryptedFieldModelBinder decrypts the received encrypted models from the clients.
        /// </summary>
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var logger              = bindingContext.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>();
            var fallbackBinder      = new SimpleTypeModelBinder(bindingContext.ModelType, logger);
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                return(fallbackBinder.BindModelAsync(bindingContext));
            }

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

            var valueAsString = valueProviderResult.FirstValue;

            if (string.IsNullOrWhiteSpace(valueAsString))
            {
                return(fallbackBinder.BindModelAsync(bindingContext));
            }

            var decryptedResult = _protectionProviderService.Decrypt(valueAsString);

            bindingContext.Result = ModelBindingResult.Success(decryptedResult);
            return(Task.CompletedTask);
        }
        public IActionResult Index()
        {
            ViewBag.Msg = "This is a test";

            ViewBag.EncryptedMsg1 = _protectionProviderService.Encrypt(ViewBag.Msg);
            ViewBag.DecryptMsg1   = _protectionProviderService.Decrypt(ViewBag.EncryptedMsg1);

            ViewBag.EncryptedMsg2 = _protectionProviderService.Encrypt(ViewBag.Msg);
            ViewBag.DecryptMsg2   = _protectionProviderService.Decrypt(ViewBag.EncryptedMsg2);

            var model = new TestModel {
                Id = 1, Name = "Test"
            };

            ViewBag.EncryptedMsg3 = _protectionProviderService.EncryptObject(model);
            ViewBag.DecryptMsg3   = _protectionProviderService.DecryptObject <TestModel>(ViewBag.EncryptedMsg3).Name;

            return(View());
        }
Exemple #4
0
        public async Task <IActionResult> Index(UserProfileViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var pid = _protectionProviderService.Decrypt(model.Pid);
                if (string.IsNullOrWhiteSpace(pid))
                {
                    return(View("Error"));
                }

                if (pid != _userManager.GetCurrentUserId() &&
                    !_roleManager.IsCurrentUserInRole(ConstantRoles.Admin))
                {
                    _logger.LogWarning($"سعی در دسترسی غیرمجاز به ویرایش اطلاعات کاربر {pid}");
                    return(View("Error"));
                }

                var user = await _userManager.FindByIdAsync(pid);

                if (user == null)
                {
                    return(View("NotFound"));
                }

                user.FirstName        = model.FirstName;
                user.LastName         = model.LastName;
                user.IsEmailPublic    = model.IsEmailPublic;
                user.TwoFactorEnabled = model.TwoFactorEnabled;
                user.Location         = model.Location;

                updateUserBirthDate(model, user);

                if (!await updateUserName(model, user))
                {
                    return(View(viewName: nameof(Index), model: model));
                }

                if (!await updateUserAvatarImage(model, user))
                {
                    return(View(viewName: nameof(Index), model: model));
                }

                if (!await updateUserEmail(model, user))
                {
                    return(View(viewName: nameof(Index), model: model));
                }

                var updateResult = await _userManager.UpdateAsync(user);

                if (updateResult.Succeeded)
                {
                    if (!model.IsAdminEdit)
                    {
                        // reflect the changes in the current user's Identity cookie
                        await _signInManager.RefreshSignInAsync(user);
                    }

                    await _emailSender.SendEmailAsync(
                        email : user.Email,
                        subject : "اطلاع رسانی به روز رسانی مشخصات کاربری",
                        viewNameOrPath : "~/Areas/Identity/Views/EmailTemplates/_UserProfileUpdateNotification.cshtml",
                        model : new UserProfileUpdateNotificationViewModel
                    {
                        User            = user,
                        EmailSignature  = _siteOptions.Value.Smtp.FromName,
                        MessageDateTime = DateTime.UtcNow.ToLongPersianDateTimeString()
                    });

                    return(RedirectToAction(nameof(Index), "UserCard", routeValues: new { id = user.Id }));
                }

                ModelState.AddModelError("", updateResult.DumpErrors(useHtmlNewLine: true));
            }
            return(View(viewName: nameof(Index), model: model));
        }
        public async Task <ApiResult <UserProfileViewModel> > Update(UserProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                var pid = _protectionProviderService.Decrypt(model.Pid);

                if (string.IsNullOrWhiteSpace(pid))
                {
                    return(BadRequest());
                }


                if (pid != _userManager.GetCurrentUserId() &&
                    !_roleManager.IsCurrentUserInRole(ConstantRoles.Admin))
                {
                    _logger.LogWarning($"سعی در دسترسی غیرمجاز به ویرایش اطلاعات کاربر {pid}");
                    return(BadRequest());
                }

                var user = await _userManager.FindByIdAsync(pid);

                if (user == null)
                {
                    return(NotFound());
                }

                user.FirstName        = model.FirstName;
                user.LastName         = model.LastName;
                user.IsEmailPublic    = model.IsEmailPublic;
                user.TwoFactorEnabled = model.TwoFactorEnabled;
                user.Location         = model.Location;

                UpdateUserBirthDate(model, user);

                if (!await UpdateUserName(model, user))
                {
                    return(model);
                }

                if (!await UpdateUserAvatarImage(model, user))
                {
                    return(model);
                }

                if (!await UpdateUserEmail(model, user))
                {
                    return(model);
                }

                var updateResult = await _userManager.UpdateAsync(user);

                if (updateResult.Succeeded)
                {
                    if (!model.IsAdminEdit)
                    {
                        // reflect the changes in the current user's Identity cookie
                        await _signInManager.RefreshSignInAsync(user);
                    }

                    await _emailSender.SendEmailAsync(
                        user.Email,
                        "اطلاع رسانی به روز رسانی مشخصات کاربری",
                        "~/Views/EmailTemplates/_UserProfileUpdateNotification.cshtml",
                        new UserProfileUpdateNotificationViewModel
                    {
                        User            = user,
                        EmailSignature  = _siteOptions.Value.Smtp.FromName,
                        MessageDateTime = DateTime.UtcNow.ToLongPersianDateTimeString()
                    });

                    return(model);
                }

                ModelState.AddModelError("", updateResult.DumpErrors(true));
            }

            return(model);
        }