public ActionResult Login(User model)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TestProjectManagerAppContext())
                {
                    User user = context.Users.FirstOrDefault(u => u.UserId == model.UserId && u.Password == model.Password);

                    if (user != null)
                    {
                        Session["UserName"] = user.UserName;
                        Session["UserId"]   = user.UserId;
                        Session["Role"]     = db.Roles.FirstOrDefault(r => r.Id == user.RoleId);
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid User Name or Password");
                        return(View(model));
                    }
                }
            }
            else
            {
                return(View(model));
            }
        }
Exemplo n.º 2
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool authorize = false;
            var  userId    = Convert.ToString(httpContext.Session["UserId"]);

            if (!string.IsNullOrEmpty(userId))
            {
                using (var context = new TestProjectManagerAppContext())
                {
                    var userRole = (from u in context.Users
                                    join r in context.Roles on u.RoleId equals r.Id
                                    where u.UserId == userId
                                    select new
                    {
                        r.Name
                    }).FirstOrDefault();
                    foreach (var role in allowedroles)
                    {
                        if (role == userRole.Name)
                        {
                            return(true);
                        }
                    }
                }
            }


            return(authorize);
        }