2012-02-22 8 views
0

管理者が教師と生徒を追加できるウェブサイトを作成しているので、管理者は教師が特定の場所にいるときにできることを指定する必要があります。MVC3余分な役割の属性

Authorize属性を拡張して、特定のユーザーの場所を確認することはできますか?たとえば、[Authorize(Roles = "Administrator"、Location = "ICT")]?

もしそうなら、これをどのように延長できますか?

ありがとうございます。

答えて

3

もしそうなら、これをどのように延長できますか?カスタム属性を承認書き込むことにより

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    public string Location { get; set; } 

    protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) 
    { 
     var result = base.AuthorizeCore(httpContext); 
     if (!result) 
     { 
      return false; 
     } 

     // At this stage we know that the currently logged in user 
     // is authorized. Now you could use the Location property 
     // to perform additional custom authorization checks and 
     // return true or false from here 

     string user = httpContext.User.Identity.Name; 

     ... 
    } 
} 

、その後:

[MyAuthorize(Roles = "Administrator", Location = "ICT")] 
+0

こんにちはダリン、どこにこのクラスを置くのですか? –

+0

@StewieFG、どこでも好きです。あなたのASP.NET MVCアプリケーションは良い場所です。カスタム許可属性などを整理するためのサブフォルダを作成することができます。 –

+0

ありがとうダーリン –

関連する問題