在我的AccountController,我有這樣的代碼:與MVC2自定義授權似乎有問題,IsInRole()
ControllerContext.HttpContext.User = new MyAppUserPrincipal(user);
當我通過這個在調試步驟我可以看到ControllerContext.HttpContext.User。 IsInRole(「Admin」)爲true。
接下來,我有一個自定義屬性保護的HomeController的:
[AuthorizeMyApp(Roles = "Admin")]
在屬性的定義,我有這樣的:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var principal = filterContext.HttpContext.User ;
if (! principal.IsInRole(_roles))
等
這裏是很奇怪登錄並嘗試返回首頁後:
principal.Identity.Name has預期的名字和IsAuthenticate是真的;然而 a)principal.IsInRole(「Admin」)爲假 b)(principal As MyAppUserPrincipal)爲空
我在這裏做錯了什麼? (使用MVC2)