ASP NET MVC authentication

Asked

Viewed 189 times

2

What better way to separate permissions for logged in users? Would it be using annotations and roles? Because I want to release certain pages and certain functions depending on the permission of the logged in user.

Someone could better inform me how it could be done and if you have any example.

The permission table I have in the database is a table with an ID and a Name only, ai in the user table has a field that gets the name of the permissions separated by comma.

  • You are using the MVC5?

  • Yes, I’m using mvc 5, and I already have a table in the oracle with the permissions, I would like to use it.

  • You can edit your question and put it like your table?

  • I edited the question.

  • 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?

  • 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.

Show 1 more comment

1 answer

2


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() { ... }
  • It helped me a lot, another doubt, is there any way I can use this authorization that you passed to a code snippet only? for example, on a page I have 2 Forms, each appears depending on the permission?

  • @Henriquedomingospereira Yes. Open another question I’ll explain in detail.

  • created http://answall.com/questions/77612/asp-net-mvc-permiss%C3%A3o-per-block

  • this class MeuAuthorize, where you recommend that I create it, I’m using DDD, and I have to give a using whenever you use it?

  • DDD is a bad approach to MVC. Anyway, if you have a directory for infrastructure, put another directory under it called Attributes.

  • Blza, and there’s some way to make it global, or I always have to call you in the file you’re using?

  • Yes. Open another question that I explain how to do.

  • Abri http://answall.com/questions/77628/autentica%C3%A7%C3%A3o-Asp-net-mvc-estendendo-authorizeattribute

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.