Exemplo n.º 1
0
        public void PutEntity(ChangeUserPasswordViewModel changeUserPasswordViewModel)
        {
            var message = new Message();
            var text    = string.Empty;

            if (changeUserPasswordViewModel.NewPassword.Equals(changeUserPasswordViewModel.ConfirmPassword))
            {
                var userId = CustomMembershipProvider.GetUserIdCookie();
                if (userId != null)
                {
                    var user     = _userService.GetUserAndUserProfileByUserId(userId ?? 0);
                    var password = _userService.GetMd5Hash(changeUserPasswordViewModel.Password);

                    if (password.Equals(user.UserProfile.Password))
                    {
                        var newPassword = _userService.GetMd5Hash(changeUserPasswordViewModel.NewPassword);
                        user.UserProfile.Password = newPassword;
                        var updatedUser = _userService.Update(user);
                        CustomMembershipProvider.SetPassCodeCookie(user.UserProfile.UserName, user.UserProfile.Password);

                        message.type = MessageType.success;
                        _constantService.TryGetValue <string>("ChangePasswordWasSuccessFull", out text);
                        message.text = text /*Core.Resources.Messages.ChangePasswordWasSuccessFull*/;
                    }
                    else
                    {
                        message.type = MessageType.error;
                        _constantService.TryGetValue <string>("IncorrectPassword", out text);
                        message.text = text /*Core.Resources.ExceptionMessage.IncorrectPassword*/;
                    }
                }
            }
            else
            {
                message.type = MessageType.error;
                _constantService.TryGetValue <string>("ConfirmPasswordWasNotMatched", out text);
                message.text = text /*Core.Resources.ExceptionMessage.ConfirmPasswordWasNotMatched*/;
            }

            MessageStrore.Add(message);
        }
Exemplo n.º 2
0
        public CaptchaViewModel GetCaptchaImage()
        {
            var    random        = new Random();
            string randomString  = RandomString(4);
            var    encryptionKey = string.Empty;

            if (_constantService.TryGetValue <string>("EncryptionKey", out encryptionKey))
            {
                string encryptedKey = randomString + "-" + encryptionKey;
                encryptedKey = EncryptionUtil.Sha1Util.Sha1HashString(encryptedKey);

                var captcha = string.Format("{0}", randomString);

                byte[] content;
                using (var mem = new MemoryStream())
                    using (var bmp = new Bitmap(130, 50, PixelFormat.Format32bppArgb))
                        using (var gfx = Graphics.FromImage((System.Drawing.Image)bmp))
                        {
                            gfx.SmoothingMode = SmoothingMode.AntiAlias;

                            // Create a graphics object for drawing.
                            Graphics g = Graphics.FromImage(bmp);
                            g.SmoothingMode = SmoothingMode.AntiAlias;
                            Rectangle rect = new Rectangle(0, 0, 150, 50);

                            // Fill in the background.
                            HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.Gray, Color.White);
                            g.FillRectangle(hatchBrush, rect);

                            // Set up the text font.
                            SizeF size;
                            float fontSize = rect.Height + 1;
                            Font  font;
                            // Adjust the font size until the text fits within the image.
                            do
                            {
                                fontSize--;
                                font = new Font("Arial", fontSize, FontStyle.Bold);
                                size = g.MeasureString("", font);
                            } while (size.Width > rect.Width);


                            StringFormat format = new StringFormat();
                            format.Alignment     = StringAlignment.Center;
                            format.LineAlignment = StringAlignment.Center;

                            // Create a path using the text and warp it randomly.
                            GraphicsPath path = new GraphicsPath();
                            path.AddString("", font.FontFamily, (int)font.Style, font.Size, rect, format);
                            PointF[] points =
                            {
                                new PointF(random.Next(rect.Width) / -2f,              random.Next(rect.Height) / 16f),
                                new PointF(rect.Width - random.Next(rect.Width) / -2f, random.Next(rect.Height) / -15f),
                                new PointF(random.Next(rect.Width) / -46f,             rect.Height - random.Next(rect.Height) / -100f),
                                new PointF(rect.Width - random.Next(rect.Width) / 12f, rect.Height - random.Next(rect.Height) / 10f)
                            };
                            Matrix matrix = new Matrix();
                            matrix.Translate(0F, 0F);
                            path.Warp(points, rect, matrix, WarpMode.Perspective, 20F);

                            hatchBrush = new HatchBrush(HatchStyle.Percent60, Color.FromArgb(99, 99, 99), Color.DarkGray);
                            gfx.FillPath(hatchBrush, path);

                            gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                            gfx.SmoothingMode     = SmoothingMode.AntiAlias;
                            var rand = new Random((int)DateTime.Now.Ticks);
                            int r, x1, yz;
                            var pen = new Pen(Color.Yellow);
                            for (int i = 1; i < 10; i++)
                            {
                                pen.Color = Color.FromArgb(
                                    (rand.Next(0, 255)),
                                    (rand.Next(0, 255)),
                                    (rand.Next(0, 255)));

                                r  = rand.Next(0, (130 / 3));
                                x1 = rand.Next(0, 230);
                                yz = rand.Next(0, 30);

                                gfx.DrawEllipse(pen, x1 - r, yz - r, r, r);
                            }

                            int m = Math.Max(rect.Width, rect.Height);
                            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
                            {
                                int x = random.Next(rect.Width);
                                int y = random.Next(rect.Height);
                                int w = random.Next(m / 50);
                                int h = random.Next(m / 50);
                                gfx.FillEllipse(hatchBrush, x, y, w, h);
                            }
                            gfx.DrawString(captcha, new Font("Arial", 32, FontStyle.Bold), Brushes.Gray, 10, 3);

                            bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);

                            content = mem.GetBuffer();
                        }
                return(new CaptchaViewModel {
                    Base64imgage = Convert.ToBase64String(content), EncryptedKey = encryptedKey
                });
            }

            else
            {
                throw new Exception("there is no encryptionKey");
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage PostEntity([FromBody] LogOnViewModel model)
        {
            string fullName = string.Empty;

            System.Net.HttpStatusCode statusCode = System.Net.HttpStatusCode.OK;
            var exceptionMsg  = string.Empty;
            var encryptionKey = string.Empty;

            if (_constantService.TryGetValue <string>("EncryptionKey", out encryptionKey))
            {
                string serverEncryptedKey = model.CaptchaCode + "-" + encryptionKey;

                serverEncryptedKey = EncryptionUtil.Sha1Util.Sha1HashString(serverEncryptedKey);


                if (serverEncryptedKey == model.HiddenId)
                {
                    if (ModelState.IsValid)
                    {
                        //string newUserName;
                        //if (AuthorizeWithDomain(model.UserName, out newUserName))
                        //if(!string.IsNullOrEmpty( model.Domain))
                        //{
                        //string domain = _constantService.All().FirstOrDefault(r =>
                        //    r.Key == "DomainName")?.Value?.Trim();
                        //if (String.IsNullOrWhiteSpace(domain))
                        //{
                        //    statusCode = System.Net.HttpStatusCode.BadRequest;

                        //    _constantService.TryGetValue<string>("DomainNotDefined", out exceptionMsg);
                        //    ModelState.AddModelError("UserIsNotValid", exceptionMsg);

                        //    return Request.CreateErrorResponse(statusCode, ModelState);
                        //}
                        //List<string> allowedRoles = _constantService.All().FirstOrDefault(r =>
                        //    r.Key == "AllowedRolesForDomainUsers")?.Value?.Split(',').Where(r => !String.IsNullOrWhiteSpace(r.Trim())).ToList();
                        //if (allowedRoles == null || allowedRoles.Count == 0)
                        //{
                        //    statusCode = System.Net.HttpStatusCode.BadRequest;
                        //    _constantService.TryGetValue<string>("NoRoleIsNotAllowed", out exceptionMsg);
                        //    ModelState.AddModelError("UserIsNotValid", exceptionMsg);
                        //    return Request.CreateErrorResponse(statusCode, ModelState);
                        //}
                        //List<string> userRoles;

                        //if (_domainAuthenticationService.Logon(model.UserName, model.Password, model.Domain, out fullName, out userRoles))
                        //{
                        //if (!allowedRoles.Any(r => userRoles.Contains(r.Trim().ToUpper())))
                        //{
                        //    statusCode = System.Net.HttpStatusCode.BadRequest;
                        //    _constantService.TryGetValue<string>("UserIsNotInPermittedRole", out exceptionMsg);
                        //    ModelState.AddModelError("UserIsNotValid", exceptionMsg);
                        //    return Request.CreateErrorResponse(statusCode, ModelState);
                        //}
                        //    }
                        //    else
                        //    {
                        //        statusCode = System.Net.HttpStatusCode.BadRequest;
                        //        _constantService.TryGetValue<string>("UserIsNotExistInDomain", out exceptionMsg);
                        //        ModelState.AddModelError("UserIsNotValid", exceptionMsg);

                        //        return Request.CreateErrorResponse(statusCode, ModelState);
                        //    }
                        //}
                        if (_membershipService.ValidateUser(model.UserName, model.Password) && _userService.HasAdminRecord())
                        {
                            //Check User Is Valid or Not...
                            if (!_userService.IsUserActive(model.UserName))
                            {
                                statusCode = System.Net.HttpStatusCode.BadRequest;
                                _constantService.TryGetValue <string>("UserIsNotActive", out exceptionMsg);
                                ModelState.AddModelError("UserIsNotValid", exceptionMsg);
                                return(Request.CreateErrorResponse(statusCode, ModelState));
                            }
                            var         modifiedUserName = model.UserName.CorrectPersianChars();
                            UserProfile userProfile      = _userProfileService.Filter(profile => profile.UserName.ToLower().Equals(modifiedUserName.ToLower())).FirstOrDefault();
                            _formsService.SignIn(userProfile, model.RememberMe, true);
                            if (userProfile != null)
                            {
                                var user = userProfile.User;
                                fullName = string.Format("{0} {1}", user.FName, user.LName);

                                //TODO: please uncomment later
                                // AddRecordToUserLog(user);
                            }
                        }
                        else
                        {
                            statusCode = System.Net.HttpStatusCode.BadRequest;
                            _constantService.TryGetValue <string>("IncorrectUserNameOrPassword", out exceptionMsg);
                            ModelState.AddModelError("wrongPassOrUsr", exceptionMsg);
                            return(Request.CreateErrorResponse(statusCode, ModelState));
                        }
                    }
                }
                else
                {
                    statusCode = System.Net.HttpStatusCode.BadRequest;
                    _constantService.TryGetValue <string>("IncorrectSecurityCode", out exceptionMsg);
                    ModelState.AddModelError("wrongCaptchaCode", exceptionMsg);
                    return(Request.CreateErrorResponse(statusCode, ModelState));
                }
            }
            else
            {
                statusCode = System.Net.HttpStatusCode.BadRequest;
                ModelState.AddModelError("encryptionKey", "there is no encryptionKey");
                return(Request.CreateErrorResponse(statusCode, ModelState));
            }

            return(Request.CreateResponse(statusCode, new { fullName = fullName, url = ConfigurationManager.AppSettings["RedirectUrlAfterLogin"] }));
        }