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?

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

…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.
It does come out of the box but I wondered if his dbcontext was correctly set up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.