protected void RegistrationClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                try
                {
                    using (GuestBookContext context = new GuestBookContext())
                    {
                        User newUser = new User();
                        newUser.FirstName = FirstName.Value;
                        newUser.LastName  = LastName.Value;
                        newUser.Email     = Email.Value;
                        newUser.Login     = Login.Value;
                        try
                        {
                            // шифруем пароль
                            newUser.Password = EncryptionUtility.EncryptData(Password.Value);
                        }
                        catch (Exception)
                        {
                            Response.Write("<h2>Возникла ошибка при шифровании!</h2>");
                        }

                        context.Users.Add(newUser);
                        context.SaveChanges();
                        // в случай успешной регистрации пользователь будет переадресован на страничку входа
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Write("<h2>Возникла ошибка при регистрации!</2>");
                }
            }
        }
 private void Registration(object obj)
 {
     if (Password == ConfirmPassword)
     {
         using (TastyAndHealthyContext context = new TastyAndHealthyContext())
         {
             if (!context.Users.Where(x => (x.Email == Email || x.Email == Name || x.Login == Email || x.Login == Name)).Any())
             {
                 User user = new User();
                 user.Login         = Name;
                 user.Email         = Email;
                 user.Password      = EncryptionUtility.EncryptData(Password);
                 user.Age           = Age;
                 user.Height        = Height;
                 user.CurrentWeight = CurrentWeight;
                 user.TargetWeight  = TargetWeight;
                 user.BloodType     = context.BloodTypes
                                      .Where(x => x.Name == BloodType).FirstOrDefault();
                 user.Sex = Sex;
                 context.Users.Add(user);
                 context.SaveChanges();
                 MainWindow mainWindow = new MainWindow(user);
                 mainWindow.Show();
                 foreach (Window el in Application.Current.Windows)
                 {
                     if (el.ToString().Contains("Registration"))
                     {
                         el.Close();
                         break;
                     }
                 }
             }
             else
             {
                 if (context.Users.Where(x => x.Email == Email).Any())
                 {
                     MessageBox.Show("User with this email already exists!!!");
                 }
                 else
                 {
                     MessageBox.Show("User with this login already exists!!!");
                 }
             }
         }
     }
     else
     {
         MessageBox.Show("Password confirmation does not match password");
     }
 }
示例#3
0
        protected override void Seed(MusicPortalContext db)
        {
            Role role = new Role {
                Name = "Admin"
            };
            Role role1 = new Role {
                Name = "User"
            };
            User user = new User {
                FirstName    = "Admin", LastName = "Admin", Email = "*****@*****.**",
                Login        = "******", Role = role, Password = EncryptionUtility.EncryptData("123456"),
                IsRegistered = true
            };

            db.Roles.Add(role);
            db.Roles.Add(role1);
            db.Users.Add(user);
            Genre[] genres = new Genre[] { new Genre {
                                               Name = "None"
                                           }, new Genre {
                                               Name = "Rock"
                                           } };
            db.Genres.AddRange(genres);
            Artist[] artists = new Artist[] {
                new Artist {
                    Name = "Queen"
                },
                new Artist {
                    Name = "Sia"
                },
                new Artist {
                    Name = "Linkin Park"
                },
                new Artist {
                    Name = "Evanescence"
                },
                new Artist {
                    Name = "Пикник"
                },
                new Artist {
                    Name = "Король и Шут"
                }
            };
            db.Artists.AddRange(artists);
            db.SaveChanges();
            base.Seed(db);
        }
示例#4
0
        private string RunHttp(string action, params string[] paraStrArr)
        {
            string content = "action=" + action + "&dataArrStr=";

            if (paraStrArr != null)
            {
                foreach (string str in paraStrArr)
                {
                    content += System.Web.HttpUtility.UrlEncode(str) + ",";
                }
                content = content.Substring(0, content.Length - 1);
            }
            try {
                return(webHttp.DoPost(evalActionUrl, encryption.EncryptData(content)));
            } catch (Exception ex) {
                onError("DoPost exception:" + content + " ,", ex);
                return(null);
            }
        }
        public async Task <ActionResult> Registration(RegistrationViewModel model)
        {
            string pattern         = @"^[a-zA-Zа-яА-Я0-9_-]+$";
            string loginPattern    = "[\\w| !\"§$% \\&/ () =\\-?\\@\\*\\.]*";
            string emailPattern    = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
            string passwordPattern = "[\\w| !\"§$% \\&/ () =\\-?\\@\\*]*";

            if (!User.Identity.IsAuthenticated)
            {
                if (String.IsNullOrEmpty(model.FirstName))
                {
                    ModelState.AddModelError("FirstName", "Required field First name.");
                }
                if (!String.IsNullOrEmpty(model.FirstName) && !Regex.IsMatch(model.FirstName, pattern))
                {
                    ModelState.AddModelError("FirstName", "The field contains invalid characters.");
                }
                if (!String.IsNullOrEmpty(model.FirstName) && (model.FirstName.Length < 2 || model.FirstName.Length > 20))
                {
                    ModelState.AddModelError("FirstName", "First name length must be between 2 and 20 characters");
                }
                if (String.IsNullOrEmpty(model.LastName))
                {
                    ModelState.AddModelError("LastName", "Required field Last name.");
                }
                if (!String.IsNullOrEmpty(model.LastName) && !Regex.IsMatch(model.LastName, pattern))
                {
                    ModelState.AddModelError("LastName", "The field contains invalid characters.");
                }
                if (!String.IsNullOrEmpty(model.LastName) && (model.LastName.Length < 2 || model.LastName.Length > 20))
                {
                    ModelState.AddModelError("LastName", "Last name length must be between 2 and 20 characters");
                }
                if (String.IsNullOrEmpty(model.Login))
                {
                    ModelState.AddModelError("Login", "Required field Login.");
                }
                if (!String.IsNullOrEmpty(model.Login) && !Regex.IsMatch(model.Login, loginPattern))
                {
                    ModelState.AddModelError("Login", "The field contains invalid characters.");
                }
                if (!String.IsNullOrEmpty(model.Login) && (model.Login.Length < 2 || model.Login.Length > 20))
                {
                    ModelState.AddModelError("Login", "Login length must be between 2 and 20 characters");
                }
                if (!repository.IsUniqueUserValue(model.Login))
                {
                    ModelState.AddModelError("Login", "A user with this login already exists.");
                }
                if (String.IsNullOrEmpty(model.Email))
                {
                    ModelState.AddModelError("Email", "Required field Email.");
                }
                if (!String.IsNullOrEmpty(model.Email) && !Regex.IsMatch(model.Email, emailPattern))
                {
                    ModelState.AddModelError("Email", "Invalid email input.");
                }
                if (!repository.IsUniqueUserValue(model.Email))
                {
                    ModelState.AddModelError("Email", "A user with this email already exists.");
                }
                if (String.IsNullOrEmpty(model.Password))
                {
                    ModelState.AddModelError("Password", "Required field Password.");
                }
                if (!String.IsNullOrEmpty(model.Password) && !Regex.IsMatch(model.Password, passwordPattern))
                {
                    ModelState.AddModelError("Password", "The field contains invalid characters.");
                }
                if (!String.IsNullOrEmpty(model.Password) && (model.Password.Length < 6 || model.Password.Length > 20))
                {
                    ModelState.AddModelError("Password", "Password length must be between 6 and 20 characters.");
                }
                if (String.IsNullOrEmpty(model.ConfirmPassword))
                {
                    ModelState.AddModelError("ConfirmPassword", "Required field Confirm password.");
                }
                if (model.Password != model.ConfirmPassword)
                {
                    ModelState.AddModelError("ConfirmPassword", "Password mismatch.");
                }
                if (ModelState.IsValid)
                {
                    User user = new User
                    {
                        FirstName    = model.FirstName,
                        LastName     = model.LastName,
                        Email        = model.Email,
                        Login        = model.Login,
                        Password     = EncryptionUtility.EncryptData(model.Password),
                        IsRegistered = false,
                        RoleId       = 2
                    };
                    if (await repository.CreateAsync <User>(user))
                    {
                        return(RedirectToAction("Login"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Error for registration");
                        return(View(model));
                    }
                }
                return(View(model));
            }
            else
            {
                return(RedirectToAction("PlayList", "Home"));
            }
        }