I have a method that returns a bool, true being the user is allowed to access the site and false being the user has not access to the site. As of right now I have a separate action that is called in each Action and if the user if the method returns false the user is directed to a view that says "access denied". I was wondering there is a better way to do this without throwing the duplicate code in each individual action any maybe replace it with one single call because the user will either be able to access all of the pages or non of the page.
Here is an example of what I have:
[HttpGet]
public ActionResult EmployeeAdd()
{
if (!GetUsersecurityLevel())
{
return RedirectToAction("NotAuthorizedForApplication");
}
...........
return View();
}
[HttpGet]
public ActionResult EmployeeEdit()
{
if (!GetUsersecurityLevel())
{
return RedirectToAction("NotAuthorizedForApplication");
}
..........
return View();
}
[HttpGet]
public ActionResult EmployeeDelete()
{
if (!GetUsersecurityLevel())
{
return RedirectToAction("NotAuthorizedForApplication");
}
..........
return View();
}
public string CurrentUserName()
{
return User.Identity.Name.Substring(User.Identity.Name.IndexOf(@"\", StringComparison.Ordinal) + 1).ToUpper();
}
Web.Config:
<system.web>
<authentication mode="Windows" />
<authorization><deny users="?"/></authorization>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime targetFramework="4.0"/>
<customErrors mode="Off" />
</system.web>
So my questions is: Can I make one single call on the GetUserSecurityLevel() which can protect all of the actions from being ran by unauthorized users?