2012-04-26 15 views
6

को सत्यापित करने के लिए विशेषता है मेरे पास Jquery के साथ एक यूआई है जो अजाक्स अनुरोध का उपयोग कर एमवीसी को कॉल करता है।एमवीसी कस्टम प्राधिकरण अनुरोध

मैं उपयोगकर्ता प्रोफाइल के खिलाफ प्रत्येक अनुरोध को मान्य करना चाहता हूं (कस्टम क्लास जिसमें खाता संख्या, आईडी आदि है)।

क्या कोई भी सुझाव दे सकता है कि कस्टम प्राधिकरण विशेषता बनाना संभव है कि दोनों अनुरोध और उपयोगकर्ता प्रोफाइल समान हैं?

मैं तो नीचे की तरह कुछ करना चाहते हैं:

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

आप अनुरोध फॉर्म से बाहर डेटा पार्स करने के लिए तैयार हैं, तो/querystring और उन्हें मान्य करें तो यह संभव हो सकता है। आपके पास अपने कस्टम प्राधिकरण विशेषता में httpContext तक पूर्ण पहुंच होगी। आपको यह मानना ​​होगा कि अगर कोई पोस्ट या क्वेरीरीस्ट्रिंग प्राप्त होता है तो फॉर्म में एक वैरिएबल "खाता संख्या" मौजूद होना चाहिए। पैरामीटर बाइंडिंग (आपके एक्शन में पैरामीटर के अनुरोध में डेटा मैपिंग) ऑनएक्शन एक्सेलिंग विधि के आसपास होगा जो बाद में प्राधिकृत है। –

+0

Yep accountID पास हो जाएगा। –

+1

http://stackoverflow.com/questions/6860686/extend-authorizeattribute-override-authorizecore-or-onauthorization (AuthorizeCore बनाम प्राधिकरण) देखें और यहां कोई ऐसा व्यक्ति है जो कुछ डेटा के लिए कुछ अनुरोध डेटा (बजट) देख रहा है यदि उपयोगकर्ता अधिकृत है या नहीं: http://stackoverflow.com/questions/5989100/asp-net-mvc-3- कस्टम- प्राधिकरण –

उत्तर

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

धन्यवाद @ डारिन डिमिट्रोव –

संबंधित मुद्दे