Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- public ActionResult Login(Login user)
- {
- if (ModelState.IsValid)
- {
- if (user.IsValid( user.USERNAME,user.PASSWORD))
- {
- FormsAuthentication.SetAuthCookie(user.USERNAME, false);
- // I WANT TO ADD ROLES HERE FOR THE USER
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ModelState.AddModelError("", "Login data is incorrect!");
- }
- }
- return View(user);
- }
- [AuthorizeUser(Roles = "MasterAdmin")]
- [OutputCache(Duration = 0, NoStore = true)]
- public class MasterCompaniesController : Controller
- {
- private DEMTContext db = new DEMTContext();
- // GET: MasterCompanies
- public ActionResult Index()
- {
- return View(db.MasterCompanys.ToList());
- }
- }
- public class AuthorizeUserAttribute:AuthorizeAttribute
- {
- //private readonly string[] allowedroles;
- protected override bool AuthorizeCore(HttpContextBase httpContext)
- {
- bool IsThisMasterAdmin = false;
- using(DEMTContext db = new DEMTContext())
- {
- string username= httpContext.User.Identity.Name;
- var map = db.MasterAdminPasswords.Where(m => m.MASTERADMINPASSWORD == username).FirstOrDefault();//FOR SIMPLIFICATION, PASSWORD IS OMITTED FOR VERIFICATION
- if (map != null && Roles.Contains("MasterAdmin"))
- {
- IsThisMasterAdmin = true;
- GenericIdentity gi = new GenericIdentity(passcode);
- GenericPrincipal gp = new GenericPrincipal(gi, new string[] { "MasterAdmin" });
- httpContext.User = gp;
- return IsThisMasterAdmin;
- }
- }
- return false;
- }
- }
- @if (User.IsInRole("MasterAdmin"))
- {
- if (User.Identity.IsAuthenticated)
- {
- <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Hello <span class="caret"></span></a>
- <ul class="dropdown-menu">
- @*<li><a href="#"><label class="label label-info">COMPANY : @Request.Cookies["COMPANYNAME"].Value</label></a></li>
- <li role="separator" class="divider"></li>
- <li><a href="#">Profile</a></li>
- <li><a href="#">Settings</a></li>
- <li role="separator" class="divider"></li>*@
- <li><a href="@Url.Action("Logout", "MasterAdmin")">Logout</a></li>
- </ul>
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement