I have the task of creating a read-only user for our ASP.Net MVC3 application. That is, they can log in, view all the data, but they cannot update the data.
I read a lot of authentication articles / frameworks, for example: Embed secure ASP.NET MVC applications or Free security configuration or Create action filters in ASP.Net MVC (and several others, I already lost links).
The problem with most approaches requiring radical changes in the domain / application. And to carry out this function, I have only one day .
We have about a hundred controllers, an average of 4 actions per controller (mainly CRUD operations), and through each of them there can be no question. It would also be easy to forget to add attributes to the new code - to introduce errors.
So far, I have come up with a global filter that negates all POST-based actions and controller actions called "Create" for a read-only user:
public class ReadOnlyFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var currentUser = HttpContext.Current.User;
if (currentUser == null || !currentUser.Identity.IsAuthenticated)
return;
if (!currentUser.IsInRole("Readonly"))
return;
if (filterContext.HttpContext.Request.HttpMethod.ToUpper() == "POST")
{
filterContext.HttpContext.Response.Redirect("~/ReadOnlyAccess");
}
if (filterContext.ActionDescriptor.ActionName == "Create")
{
filterContext.HttpContext.Response.Redirect("~/ReadOnlyAccess");
}
return;
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
}
The next thing I plan to create the [AllowReadOnlyUser] attribute for post-actions, for example, changing the password / email and filter, allows this action to pass.
I wonder if there are better ways to do such things?
Update: The application is not for public consumption. It is used in the corporate world to track people, assets, and other boring data.
2. , . , . , .