2012-04-26 13 views
6

Ajax isteğini kullanarak MVC'ye çağrı yapan JQuery ile bir UI alıyorum.MVC Özel İstek Onayı doğrulamak için Öznitelik

Her bir isteği userProfile (hesap numarası, kimlik vb. Tutan özel sınıf) ile karşılaştırmak istiyorum.

Herkes, istek ve kullanıcı profilinin aynı olduğunu doğrulamak için özel Yetkilendirme Özniteliği oluşturmanın mümkün olup olmadığını önerebilir mi?

Sonra aşağıda böyle bir şey yapmak istiyorum:

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

Talep formu ile dışarı veri ayrıştırmak için istekli iseniz/Querystring ve onları doğrulayın o zaman mümkün olabilir. Özel yetkilendirme özelliğinizdeki httpContext'e tam erişebileceksiniz. Bir POST veya bir GET ise QueryString ise, formda bir "accountNumber" değişkeninin bulunması gerektiğini varsaymanız gerekir. Parametre bağlanması (isteğinizle eyleminizdeki parametrelere eşleme verileri), Yetkilendirme sonrası olan OnActionExecuting yöntemi etrafında gerçekleşecektir. –

+0

Yep hesap kimliği geçilecek. –

+1

http://stackoverflow.com/questions/6860686/extend-authorizeattribute-override-authorizecore-or-onauthorization (AuthorizeCore vs OnAuthorize) 'a bakın ve bazı veriler için bazı Veriler verilerini (bütçe) inceleyen bir kişi var. Kullanıcı Yetkili veya Yetkili değilse: http://stackoverflow.com/questions/5989100/asp-net-mvc-3-custom-authorisation –

cevap

17

Özel bir yetkilendirme yazabilirsiniz özellik:

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

Teşekkür ederiz @Darin Dimitrov –

İlgili konular