はい、あなたはこれを行うことができます。
Authorizeは、IPrincipalのIsInRoleメソッドを使用して、ユーザーが特定のロール内にあるかどうかを判別します。
Global.asax内のAuthenticateRequestイベント中に、これを処理する実装を使用して、IPrincipalのデフォルトの実装を切り替えることができます。
実際に機能してコンパイルされ、Webサイトがハッカーによる攻撃にさらされない可能性のあるサンプルコードを次に示します。
private void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (!Request.IsAuthenticated)
{
Context.User = new MyPrincipal { Identity = new MyIdentity
{ Type = UserType.Inactive, Id = int.MinValue }};
Thread.CurrentPrincipal = Context.User;
}
else
{
HttpCookie authCookie = Request.Cookies[
FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
var identity = Db.GetIdentity(
authTicket.Name, new HttpRequestWrapper(Request));
Context.User = new MyPrincipal { Identity = new MyIdentity
{ Type = UserType.Inactive, Id = int.MinValue }};
Thread.CurrentPrincipal = Context.User;
}
}
}