2

我使用WebSecurity進行身份驗證,並使用自定義ClaimsAuthorizationManager進行授權。ASP.NET MVC 4讀取持久性Auth Cookie事件?

混合基於聲明的授權與內置的認證WebSecurity功能爲我提供了大量的價值。我強烈建議任何需要複雜的授權邏輯組合的系統。

無論如何,除了RememberMe功能,一切都很好。

當用戶登錄時,我將自己的身份驗證Cookie(通過WebSecurity)設置爲新的我的ClaimsPrincipal,並將其寫入我的SessionSecurityToken。巴姆,它工作出色。

但是,當用戶以前選擇堅持(Websecurity)身份驗證cookie時,她被允許繞過我的登錄方法,該消息向上我的ClaimsPrincipal並將我的主體寫入我的SessionSecurityToken。我的授權失敗,因爲我的申請沒有被加載,因爲我沒有機會改變我的ClaimsPrincipal

有沒有辦法掛鉤到(Websecurity)「表單身份驗證Cookie讀取」事件?如果是這樣,我可以處理它,新的我的ClaimsPrincipal,並在我的路上。提前致謝!

回答

2

你可以寫一個自定義的AuthorizeAttribute

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (authorized) 
     { 
      httpContext.User = new ClaimsPrincipal(...) 
     } 

     return authorized; 
    } 
} 

現在裝飾你的保護,與此自定義控制器動作,而不是屬性的默認內置:

[MyAUthorize] 
public ActionResult Protected() 
{ 
    ... 
} 
+0

熱賣!咄。謝謝你,先生。這很好。 – kmehta

+0

@Darin,不錯。如果我使用全局過濾器默認阻止了所有內容,並且我僅爲「登錄」控制器添加了「允許匿名」白名單?我想同樣的原則,在F​​ilterConfig類我會註冊filters.Add(新MyAUthorizeAttribute())。正確嗎? – joedotnot