2016-01-21 78 views
0

我正在嘗試使用我的自定義管理器進行基於角色的授權。我有用戶表和角色表。該模型看起來像這樣Asp.Net MVC C#[授權(角色「」)]不起作用

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

我能夠添加角色ID在User.Roles列表。所以我創建一個名爲admin的角色並將其添加到用戶列表中。它已成功添加。

然後我嘗試這在我的控制器

[Authorize(Roles = "admin")] 

之一,但它不工作。我錯過了什麼嗎?

回答

0

您是否添加了一個roleprovider以及其他需要它的工作?

還有就是具有良好的HOWTO類似的問題開始使用這樣的:Creating a AuthorizeAttribute - what do I need to know?

+0

......爲什麼呢? ASP.NET身份不帶有自己的內置角色提供程序嗎?它什麼時候被剝離?我想知道,因爲只有在我的最新項目中,授權(角色=「角色」)已停止運行 - 在以前的所有項目中,它的功能完美無缺,無需創建或添加角色提供程序。 ASP.NET 4.5.2,MVC 5. –

+0

它確實出來,但我想知道他的dbcontext是否設置正確。 – RoteS