Exemplo n.º 1
0
        protected void ValidateUser(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                String pass = CryptoProvider.GetMD5Hash(PasswordTb.Text);
                try
                {
                    using (Storage db = new Storage())
                    {
                        var currentUser = db.Users.Where(u => u.Login == LoginTb.Text && u.Password == pass && u.Status == true).Select(u => new { Login = u.Login, Role = u.UserRole.Name }).FirstOrDefault();


                        if (currentUser != null)
                        {
                            Session["ActiveUser"] = currentUser.Login;
                            Session["UserRole"]   = currentUser.Role;

                            FormsAuthentication.RedirectFromLoginPage(LoginTb.Text, StayInChb.Checked);
                        }
                        else
                        {
                            ErroMessage.Text = "Не правильный логин или пароль";
                        }
                    }
                }
                catch (Exception ex)
                {
                    Loger.Log(Response, ex);
                }
            }
        }
Exemplo n.º 2
0
        protected void CreateUserBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    using (Storage db = new Storage())
                    {
                        if (!db.Users.Where(u => u.Login == LoginTb.Text).Any())
                        {
                            DAL.User new_user = new User();

                            new_user.Login    = LoginTb.Text;
                            new_user.Name     = (string.IsNullOrEmpty(NameTb.Text) || string.IsNullOrWhiteSpace(NameTb.Text)) ? null : NameTb.Text;
                            new_user.Surname  = (string.IsNullOrEmpty(SurnameTb.Text) || string.IsNullOrWhiteSpace(SurnameTb.Text)) ? null : SurnameTb.Text;
                            new_user.Password = CryptoProvider.GetMD5Hash(PasswordTb.Text);
                            new_user.Status   = StatusList.SelectedValue == "0" ? false : true;
                            new_user.UserRole = db.Roles.Where(r => r.Name == RoleList.SelectedValue).First();

                            db.Users.Add(new_user);
                            db.SaveChanges();

                            ResetUserCreateForm();
                            UserGridView.DataBind();
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Пользователь был успешно добавлен')", true);
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Пользователь с таким логином уже существует')", true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Loger.Log(Response, ex);
                }
            }
        }