2012-04-26 12 views
6

私はAjaxリクエストを使用してMVCを呼び出すJqueryでUIを持っています。リクエストを検証するMVCカスタム許可属性

私はuserProfile(アカウント番号、IDなどを保持するカスタムクラス)に対して各リクエストを検証したいと思います。

リクエストとuserprofileの両方が同じであることを検証するカスタム認証属性を作成できるかどうかを教えてください。

私はその後、以下のような何かをしたいと思います:

[AuthorizeUser] 
public ActionResult GetMyConsumption(string accountNumber) 
{ 
    ..... 
    return View(); 
} 
+0

をクエリーストリングを実行し、それらを検証することができます。カスタム権限属性でhttpContextに完全にアクセスできます。 POSTの場合は変数 "accountNumber"がフォームに、GETの場合はQueryStringがなければならないと仮定する必要があります。パラメータのバインド(リクエスト内のデータをアクション内のパラメータにマッピングする)は、承認後のOnActionExecutingメソッドの周りで発生します。 –

+0

はい、accountIDが渡されます。 –

+1

http://stackoverflow.com/questions/6860686/extend-authorizeattribute-override-authorizecore-or-onauthorization(AuthorizeCoreとOnAuthorize)をチェックしてください。ここでは、いくつかのデータのためのリクエストデータ(予算)を見ている人がいますユーザーが承認されているかどうか:http://stackoverflow.com/questions/5989100/asp-net-mvc-3-custom-authorisation –

答えて

17

あなたは属性オーソライズカスタムを書くことができます:あなたが要求フォームからデータを解析するために喜んでいる場合は/

public class AuthorizeUserAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var isAuthorized = base.AuthorizeCore(httpContext); 
     if (!isAuthorized) 
     { 
      // The user is not authorized => no need to continue 
      return false; 
     } 

     // At this stage we know that the user is authorized => we can fetch 
     // the username 
     string username = httpContext.User.Identity.Name; 

     // Now let's fetch the account number from the request 
     string account = httpContext.Request["accountNumber"]; 

     // All that's left is to verify if the current user is the owner 
     // of the account 
     return IsAccountOwner(username, account); 
    } 

    private bool IsAccountOwner(string username, string account) 
    { 
     // TODO: query the backend to perform the necessary verifications 
     throw new NotImplementedException(); 
    } 
} 
+0

@ダリン・ディミトロフ –

関連する問題