Guest User

Untitled

a guest
Feb 2nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class AuthorizeUserAttribute : AuthorizeAttribute
  2. {
  3. protected override bool AuthorizeCore(HttpContextBase httpContext)
  4. {
  5. var isAuthorized = base.AuthorizeCore(httpContext);
  6. if (!isAuthorized)
  7. {
  8. return false;
  9. }
  10. string roles = string.Join("", httpContext.Session["UserRole"]);
  11. // string roles = string.Join("", HttpContext.Current.Session["UserRole"]);
  12. if (Roles.Contains(roles))
  13. {
  14. return true;
  15. }
  16. else
  17. {
  18. return false;
  19. }
  20. }
  21.  
  22. public ActionResult LogIn()
  23. {
  24. var model = new UserModel();
  25. return View(model);
  26. }
  27.  
  28.  
  29. [HttpPost]
  30. public ActionResult LogIn(UserModel model)
  31. {
  32.  
  33. if (!ModelState.IsValid)
  34. {
  35. return View("LogIn", model);
  36. }
  37. else
  38. {
  39. var usermodelDB = _UserAccountService.GetUser(model.Password);
  40. if (model.userName == usermodelDB.userName && model.Password==usermodelDB.Password)
  41. {
  42.  
  43.  
  44. model.userRole = usermodelDB.userRole;
  45. FormsAuthentication.SetAuthCookie(model.userRole, true);
  46. System.Web.HttpContext.Current.Session["UserRole"] = usermodelDB.userRole;
  47. var ia =System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
  48. }
  49. return View("LogIn", model);
  50. }
  51. }
  52.  
  53. [AuthorizeUser(Roles="User")]
  54. public ActionResult Index(int page=0)
  55. {
  56. return View());
  57. }
Add Comment
Please, Sign In to add comment