Пример #1
0
        //public CustomRoleProvider(DataContext context)
        //{
        //    if (context != null)
        //    {
        //        Context = context;
        //    }
        //    else
        //    {
        //        Context = new DataContext();
        //    }

        //}
        public void CreateRole(string roleName, string description)
        {
            Role Role = null;

            Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
            if (Role == null)
            {
                Role NewRole = new Role
                {
                    RoleName    = roleName,
                    Description = description
                };
                Context.Roles.Add(NewRole);
                Context.SaveChanges();
            }
        }
Пример #2
0
        public bool CreateUser(string username, string password, string email)
        {
            try
            {
                User NewUser = new User
                {
                    Username   = username,
                    Password   = password,
                    Email      = email,
                    CreateDate = DateTime.UtcNow,
                };

                Context.Users.Add(NewUser);
                Context.SaveChanges();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #3
0
        public ActionResult UnlockerConfirmed(int id)
        {
            var    name     = db.Users.ToList().Where(b => b.Email == WebSecurity.CurrentUserName);
            string username = "";

            foreach (var p in name)
            {
                username = p.Username;
            }
            var a = db.Users.Find(id);

            a.LockCount = 0;
            db.Users.AddOrUpdate(a);
            db.SaveChanges(username);
            System.Web.HttpContext.Current.Session["NOT"] = "You have successfully unlocked the account";

            return(RedirectToAction("Index"));
        }
Пример #4
0
        public ActionResult Index(LoginViewModel model, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                model.Password = ComputeHash(model.Password, new SHA256CryptoServiceProvider());
                var user = Context.Users.Where(u => u.Username.ToLower() == model.Username.ToLower() && u.Password == model.Password && u.LockCount == 0).FirstOrDefault();
                if (user != null)
                {
                    var roles = user.Roles.Select(m => m.RoleName).ToArray();

                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserId    = user.UserId;
                    serializeModel.FirstName = user.FirstName;
                    serializeModel.LastName  = user.LastName;
                    serializeModel.roles     = roles;
                    string role     = user.role;
                    string userData = JsonConvert.SerializeObject(serializeModel);
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                        1,
                        user.Email,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(60),
                        false,
                        userData);

                    string     encTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                    Response.Cookies.Add(faCookie);



                    if (roles.Contains(role))
                    {
                        counter = 0;
                        return(Redirect("~/" + role + "/Index"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Account"));
                    }
                }
                if (counter == 0)
                {
                    var    users = Context.Users.ToList().Where(a => a.Username == model.Username);
                    string name  = "";
                    foreach (var p in users)
                    {
                        name = p.Username;
                    }
                    if (name == model.Username)
                    {
                        lock (lockObj)
                        {
                            counter++;
                        }
                        System.Web.HttpContext.Current.Session["Status"] = model.Username;
                    }
                }
                else
                {
                    var    users = Context.Users.ToList().Where(a => a.Username == model.Username);
                    string name  = "";
                    foreach (var p in users)
                    {
                        name = p.Username;
                    }
                    if (name == model.Username)
                    {
                        if (name == System.Web.HttpContext.Current.Session["Status"].ToString())
                        {
                            lock (lockObj)
                            {
                                counter++;
                            }
                        }
                        else
                        {
                            counter = 1;
                            System.Web.HttpContext.Current.Session["Status"] = model.Username;
                        }
                    }
                }



                var    locks = Context.Users.ToList().Where(a => a.Username == model.Username && a.LockCount != 0);
                string name2 = "";

                foreach (var x in locks)
                {
                    name2 = x.Username;
                }
                if (name2 == null)
                {
                    name2 = "@";
                }

                if (name2 == model.Username)
                {
                    ModelState.Clear();
                    ViewBag.Attempts = "Contact the administrator you have been locked out";
                }
                else
                {
                    if (counter > 3)
                    {
                        var users = Context.Users.ToList().Where(b => b.Username == model.Username);
                        int id    = 0;
                        foreach (var p in users)
                        {
                            id = p.UserId;
                        }
                        //lock the user

                        var a = Context.Users.Find(id);
                        a.LockCount = 5;
                        Context.Users.AddOrUpdate(a);
                        Context.SaveChanges("SystemLock");
                        ViewBag.Attempts = "Contact the administrator you have been locked out";
                    }
                    else
                    {
                        ModelState.Clear();
                        ViewBag.Attempts = "Number of Login attempts " + counter;
                    }
                }
                ModelState.AddModelError("", "Incorrect username and/or password");
            }
            model.Username = "";
            model.Password = "";
            return(View(model));
        }