0

I am trying to make role based authorization with my custom manager. I have User Table and Roles Table. The models looks like this

public class Account : EntityModel
{
    public string Username { get; set; }

    public string PasswordHash { get; set; }

    public string Email { get; set; }

    public Account()
        : base("1", Suid.NewSuid())
    {
        Roles = new List<String>();
    }

    public static IEnumerable<Account> GetAll()
    {
        return TableHelper.GetAll<Account>();
    }

    public List<String> Roles { get; set; }

    public Account Save()
    {
        TableHelper.Save<Account>(this);
        return this;
    }

}

Roles Model looks like this

public class Role : EntityModel
{
    public string Name { get; set; }
    public String Id
    {
        get
        {
            return this.RowKey;
        }
        set
        {
            this.RowKey = value;
        }
    }

    public Role()
        : base("1", Suid.NewSuid())
    {

    }

    public static IEnumerable<Role> GetAll()
    {
        return TableHelper.GetAll<Role>();
    }

    public static Role Get(string x)
    {
        return TableHelper.Get<Role>("1", x);
    }
}

I am able to add Role Id in User.Roles list. So I create a role named admin and add it to the users list. It is added successfully.

Then I try this on one of my controllers

[Authorize(Roles = "admin")]

but it doesn't work. Am I missing something?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

1 Answers1

0

Did you add a roleprovider and the rest that is needed for it to work?

There is a similar question which has a good howto to get started with this: Creating a AuthorizeAttribute - what do I need to know?

Community
  • 1
  • 1
RoteS
  • 1,405
  • 13
  • 10
  • …Why? Does ASP.NET Identity not come with its own built-in roleprovider? When was it stripped out? I would like to know, because it is only on my latest project that `Authorize(Roles="role")` has ceased to function -- in all prior projects, it has functioned flawlessly without any need to create or add a roleprovider. ASP.NET 4.5.2, MVC 5. – René Kåbis Jul 04 '16 at 22:59
  • It does come out of the box but I wondered if his dbcontext was correctly set up. – RoteS Jul 05 '16 at 06:10