【问题标题】:Asp.Net MVC C# [Authorize(Roles"")] not workingAsp.Net MVC C# [Authorize(Roles"")] 不工作
【发布时间】:2023-03-11 23:38:01
【问题描述】:

我正在尝试使用我的自定义经理进行基于角色的授权。我有用户表和角色表。模型看起来像这样

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;
    }

}

角色模型是这样的

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);
    }
}

我可以在User.Roles 列表中添加角色 ID。所以我创建了一个名为admin 的角色并将其添加到用户列表中。添加成功。

然后我在我的一个控制器上试试这个

[Authorize(Roles = "admin")]

但它不起作用。我错过了什么吗?

【问题讨论】:

    标签: c# asp.net azure


    【解决方案1】:

    您是否添加了 roleprovider 以及它工作所需的其余部分?

    有一个类似的问题有一个很好的入门方法:Creating a AuthorizeAttribute - what do I need to know?

    【讨论】:

    • …为什么? ASP.NET Identity 没有自带内置的角色提供者吗?什么时候被剥离的?我想知道,因为只有在我的最新项目中,Authorize(Roles="role") 才停止运行——在之前的所有项目中,它运行完美,无需创建或添加角色提供程序。 ASP.NET 4.5.2,MVC 5。
    • 它确实是开箱即用的,但我想知道他的 dbcontext 是否设置正确。
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 2021-01-11
    • 2013-12-24
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多