カスタム属性を記述し、カスタム認証ロジックを配置できるメソッドAuthorize
をオーバーライドできます。AuthorizeCore
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authroized = base.AuthorizeCore(httpContext);
if (!authroized)
{
return false;
}
// at this stage the base authorization process has passed.
// now implement your custom authorization logic and return true or false
// here you have access to the HttpContext and as a consequence to all
// request and route parameters so you could implement any
// authorization logic you want
// And of course if you want a completely custom authorization logic
// ignoring the base functionality don't call the base method above
// but completely override anything here
}
}
あとは、対応するコントローラー/アクションをこのカスタム属性で装飾するだけです。