2015-06-04 50 views
0

我目前將我的代碼從ASP NET MVC 4升級到5.我在添加OWIN安全性後出現問題,下面是我使用的代碼。授權錯誤,'RoleEntity不是當前上下文模型的一部分'

[Authorize(Roles="Admin")] 
    public async Task<string> test() 
    { 
     var role = ""; 
     using (
     var userManager = 
      new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 
     { 
      var rolesForUser = await userManager.GetRolesAsync(User.Identity.GetUserId()); 
      return "test " + User.Identity.GetUserName() + " " + rolesForUser.ElementAt(0); 

     } 

    } 

其送花兒給人給我的錯誤

的實體類型RoleEntity是不是該機型爲當前上下文的一部分。

當我使用[Authorize(Users="username")]時,它正常工作,它只在將'Users'更改爲'Roles'時出錯。我的數據庫是SQLServer的使用實體框架5.

回答

0

我發現如何解決我自己的問題,

默認[授權(角色=「管理員」)是參照的System.Web .Mvc,應當參考System.Web.Http

,所以我更改代碼以[System.Web.Http.Authorize(角色= 「管理員」)]

我希望它能幫助一些和我一樣有問題的人。

相關問題