2011年2月22日火曜日

.NET MVC | 認証の方法(Authorize)

認証の方法は以下のとおり。

通常の認証

  1. // コントローラクラスで認証設定  
  2. [Authorize]  
  3. public ActionResult Index()  
  4. {              
  5.   return View();  
  6. }  

カスタム認証

  1. // カスタムクラスの作成  
  2. public class CustomAuthorizeAttribute : AuthorizeAttribute  
  3. {  
  4.  protected override bool AuthorizeCore(HttpContextBase httpContext)  
  5.  {  
  6.   // ・・・認証処理を記述する・・・  
  7.   return true;  
  8.  }  
  9. }  
  1. // コントローラクラスで認証設定  
  2. [CustomAuthorize]  
  3. public ActionResult Index()  
  4. {              
  5.   return View();  
  6. }  

という感じで、AuthorizeAttributeクラスを継承してやるとできる。

参考
SCHOTIME.NET
David Hayden

0 件のコメント:

コメントを投稿