Basically, by writing your own authorization attribute. For example:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class MeuAuthorize : AuthorizeAttribute
{
private String[] _permissoes;
private MeuProjetoContext contexto = new MeuProjetoContext();
public CustomAuthorizeAttribute(params String[] permissoes)
{
_permissoes = permissoes;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var baseReturn = base.AuthorizeCore(httpContext);
var permissoesReturn = false;
var permissoesUsuario = contexto.Permissoes.Where(p => p.Usuario.Nome == httpContext.User.Identity.Name).Select(p => NomePermissao).ToList();
permissoesReturn = permissoesUsuario.Intersect(_permissoes.OfType<String>().ToList()).Any();
return permissionsReturn || baseReturn;
}
}
Use:
[MeuAuthorize("Usuário", "Gerente", "Administrador")]
public ActionResult MinhaAction() { ... }
Can be used without parameters, just to check if you are logged in:
[MeuAuthorize]
public ActionResult MinhaAction() { ... }
You are using the MVC5?
– Leonel Sanches da Silva
Yes, I’m using mvc 5, and I already have a table in the oracle with the permissions, I would like to use it.
– henriquedpereira
You can edit your question and put it like your table?
– Leonel Sanches da Silva
I edited the question.
– henriquedpereira
It is a bad approach to the user table. It should be an associative table between users and permissions. It is possible to change this?
– Leonel Sanches da Silva
I already got the system like this, but I can change yes, I wanted to have an idea of how it can be done in Asp net mvc, then I change the bank.
– henriquedpereira