Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult Login(Login user)
  3. {
  4. if (ModelState.IsValid)
  5. {
  6. if (user.IsValid( user.USERNAME,user.PASSWORD))
  7. {
  8. FormsAuthentication.SetAuthCookie(user.USERNAME, false);
  9. // I WANT TO ADD ROLES HERE FOR THE USER
  10. return RedirectToAction("Index", "Home");
  11. }
  12. else
  13. {
  14. ModelState.AddModelError("", "Login data is incorrect!");
  15. }
  16. }
  17. return View(user);
  18. }
  19.  
  20. [AuthorizeUser(Roles = "MasterAdmin")]
  21. [OutputCache(Duration = 0, NoStore = true)]
  22. public class MasterCompaniesController : Controller
  23. {
  24. private DEMTContext db = new DEMTContext();
  25.  
  26. // GET: MasterCompanies
  27. public ActionResult Index()
  28. {
  29. return View(db.MasterCompanys.ToList());
  30. }
  31. }
  32.  
  33. public class AuthorizeUserAttribute:AuthorizeAttribute
  34. {
  35. //private readonly string[] allowedroles;
  36.  
  37.  
  38. protected override bool AuthorizeCore(HttpContextBase httpContext)
  39. {
  40.  
  41.  
  42. bool IsThisMasterAdmin = false;
  43.  
  44. using(DEMTContext db = new DEMTContext())
  45. {
  46. string username= httpContext.User.Identity.Name;
  47.  
  48. var map = db.MasterAdminPasswords.Where(m => m.MASTERADMINPASSWORD == username).FirstOrDefault();//FOR SIMPLIFICATION, PASSWORD IS OMITTED FOR VERIFICATION
  49. if (map != null && Roles.Contains("MasterAdmin"))
  50. {
  51. IsThisMasterAdmin = true;
  52. GenericIdentity gi = new GenericIdentity(passcode);
  53. GenericPrincipal gp = new GenericPrincipal(gi, new string[] { "MasterAdmin" });
  54. httpContext.User = gp;
  55. return IsThisMasterAdmin;
  56. }
  57. }
  58. return false;
  59. }
  60. }
  61.  
  62. @if (User.IsInRole("MasterAdmin"))
  63. {
  64. if (User.Identity.IsAuthenticated)
  65. {
  66. <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Hello <span class="caret"></span></a>
  67. <ul class="dropdown-menu">
  68. @*<li><a href="#"><label class="label label-info">COMPANY : @Request.Cookies["COMPANYNAME"].Value</label></a></li>
  69. <li role="separator" class="divider"></li>
  70. <li><a href="#">Profile</a></li>
  71. <li><a href="#">Settings</a></li>
  72. <li role="separator" class="divider"></li>*@
  73. <li><a href="@Url.Action("Logout", "MasterAdmin")">Logout</a></li>
  74. </ul>
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement