2013-07-24 7 views
5

सत्र में उपयोगकर्ता की जानकारी रखने के बाद, सत्र की जांच करना कितना संभव है और प्रदान किए गए मानों में से किसी एक के आधार पर कस्टम विशेषता के साथ सजाए गए तरीके तक पहुंच की अनुमति दें।कस्टम विशेषताओं के साथ उपयोगकर्ता पहुंच की जांच करें

तो मुझे क्या करना कोशिश कर रहा हूँ है:

public class UserAccess: System.Attribute 
{ 
    private string userRole; 

    public UserAccess(string userRole) 
    { 
     this.userRole = userRole; 

    } 
} 

फिर जब मैं इस तरह कि समाप्ति बिंदु को सजाने:

[UserAccess(userRole = "Residents")] 
public Response Get(Request r){ 
    ///-- Implementation 
} 

किसी तरह जब endpoint शुरू हो जाती है केवल userRole = "Residents" वास्तव में निष्पादित यह आधारित कर सकते हैं एक सत्र मूल्य जांच पर। साथ ही, क्या यह सत्यापन कस्टम विशेषता कार्यान्वयन में किया जा सकता है?

उत्तर

17

तो अन्य लोग सही हैं, कि गुण स्वयं कुछ भी नहीं करते हैं। यह सिर्फ मेटाडाटा है कि आपको जानबूझकर सेवा कॉल के जीवनकाल के दौरान किसी बिंदु पर जाना है।

ऐसा करने का सबसे अच्छा तरीका यह है कि यह ऑटो-जादुई तरीके से किया जाता है और हमेशा हर ऑपरेशन में सीधे इंस्पेक्टरों और सेवा व्यवहार को जोड़ना नहीं होता है। प्रारंभ में सेटअप करने के लिए यह अधिक काम है, लेकिन यह आपके प्रत्यक्ष ऑपरेशन कोड से बाहर हो जाता है और यह उस कस्टम विशेषता की जांच के लिए किसी भी ऑपरेशन के लिए आवेदन कर सकता है।

मूल रूप से तुम इतने तरह अपनी विशेषता है:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using System.ServiceModel.Dispatcher; 
using System.Web; 

namespace MyCustomExtensionService 
{ 
    public class MyParameterInspector : IParameterInspector 
    { 

     public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) 
     { 
      //throw new NotImplementedException(); 
     } 

     public object BeforeCall(string operationName, object[] inputs) 
     { 
      MethodInfo method = typeof(Service1).GetMethod(operationName); 
      Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(UserAccessAttribute), true); 

      var attr = (UserAccessAttribute)attributes.First(); 

      if (attributes.Any()) 
      { 
       var userHasProperAuthorization = true; 
       if (attr.GetUserRole() == "Residents" && userHasProperAuthorization) 
       { 
        //everything is good, continue to operation 
       } 
       else 
       { 
        throw new FaultException("You do not have the right security role!"); 
       } 
      } 



      return null; 

     } 
    } 
} 

तो फिर तुम सेटअप अपने अंतिम बिंदु व्यवहार:

namespace MyCustomExtensionService 
{ 
    public class UserAccessAttribute : System.Attribute 
    { 
     private string _userRole; 

     public UserAccessAttribute(string userRole) 
     { 
      _userRole = userRole; 

      //you could also put your role validation code in here 

     } 

     public string GetUserRole() 
     { 
      return _userRole; 
     } 
    } 
} 

तो फिर आप अपने पैरामीटर निरीक्षक (ध्यान दें अन्य निरीक्षकों आप इस्तेमाल कर सकते हैं देखते हैं) की स्थापना (ऐसे अन्य व्यवहार भी हैं जिनका आप उपयोग कर सकते हैं):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel.Description; 
using System.ServiceModel.Dispatcher; 
using System.Web; 

namespace MyCustomExtensionService 
{ 
    public class MyCustomAttributeBehavior : IEndpointBehavior 
    { 
     public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
     { 
      //throw new NotImplementedException(); 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
     { 
      foreach (ClientOperation clientOperation in clientRuntime.Operations) 
      { 
       clientOperation.ParameterInspectors.Add(
        new MyParameterInspector()); 
      } 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) 
     { 
      foreach (DispatchOperation dispatchOperation in endpointDispatcher.DispatchRuntime.Operations) 
      { 

       dispatchOperation.ParameterInspectors.Add(
        new MyParameterInspector()); 
      } 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
      //throw new NotImplementedException(); 
     } 
    } 
} 

फिर आप y हमारे व्यवहार अनुभाग:

using System.Linq; 
using System.ServiceModel.Configuration; 
using System.Web; 

namespace MyCustomExtensionService 
{ 
    public class MyBehaviorSection : BehaviorExtensionElement 
    { 

     protected override object CreateBehavior() 
     { 
      return new MyCustomAttributeBehavior(); 

     } 

     public override Type BehaviorType 
     { 

      get { return typeof(MyCustomAttributeBehavior); } 


     } 
    } 
} 

तो फिर तुम सेटअप config नए व्यवहार का उपयोग करने के: - एक ऑपरेशन के साथ कि काम करते हैं और एक है कि वजह से होने के लिए असफल हो जायेगी जाएगा

<system.serviceModel> 
    <services> 
     <service name ="MyCustomExtensionService.Service1"> 
     <endpoint address="" behaviorConfiguration="MyCustomAttributeBehavior" 
      binding="basicHttpBinding" contract="MyCustomExtensionService.IService1"> 
     </endpoint> 
     </service> 
    </services> 
    <extensions> 
     <behaviorExtensions> 
     <add name="Validator" type="MyCustomExtensionService.MyBehaviorSection, MyCustomExtensionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
     </behaviorExtensions> 
    </extensions> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="MyCustomAttributeBehavior"> 
      <Validator /> 
     </behavior> 
     </endpointBehaviors> 
यहाँ

सेवाओं इंटरफेस है गलत उपयोगकर्ता पहुँच

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace MyCustomExtensionService 
{ 

    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     string GetDataUsingWrongUserAccess(int value); 

    } 



} 

और सेवा संचालन:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace MyCustomExtensionService 
{ 

    public class Service1 : IService1 
    { 
     [UserAccess("Residents")] 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     [UserAccess("Admin")] 
     public string GetDataUsingWrongUserAccess(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 
    } 
} 

अधिक जानकारी के लिए, निरीक्षकों के लिए MSDN http://msdn.microsoft.com/en-us/library/ms730137.aspx

यह भी देखें: http://cgeers.com/2008/11/09/wcf-extensibility-parameter-inspectors/

+0

वाह .. यह बहुत कोड है .. धन्यवाद – user1791567

+1

ईश्वर आपको इतनी व्यापक प्रतिक्रिया के लिए आशीर्वाद देता है। – Jeremy

0

नहीं, आप ऐसा नहीं कर सकते (स्वयं नहीं), गुण आपके कोड में संकलित मेटाडेटा से अधिक कुछ नहीं हैं, स्वयं द्वारा वे कुछ भी नहीं करते हैं। एक बार जब आप कुछ विशेषताओं को मेटाडाटा के साथ एक विधि या वर्ग को सजाने, तुम तो प्रतिबिंब का उपयोग कर सकते, GetCustomAttributes(typeof(UserAccess)) की तरह, मेटाडाटा और इस पर कार्य को पुनः प्राप्त करने, इस SO answer दिखाता है यह बहुत अच्छी तरह से

आप क्या कर सकते है, एक कस्टम विधि बनाने जो मेटाडेटा को पुनः प्राप्त करने के लिए प्रतिबिंब का उपयोग करेगा और उसके बाद मूल्यांकन करेगा, फिर public Response Get(Request r) के अंदर, आप कुछ भी करने से पहले इस विधि को बुला सकते हैं, लेकिन यह बिल्कुल सही प्रकार का स्वैच्छिक मूल्यांकन नहीं है जिसे आप

+0

सहमत .. मुझे लगता है कि मैं बेहतर सत्र से बाहर वस्तु हो और उसका मूल्यांकन ... – user1791567

1

विशेषताएँ हैं झंडे, विवरण, अतिरिक्त जानकारी जैसे मेटाडेटा। आपको इस जानकारी को स्वयं संसाधित करने की आवश्यकता है। आप इसे विधि में ही कर सकते हैं या प्रतिबिंब का उपयोग कर इसे संसाधित करने के लिए कुछ सहायक वर्ग कर सकते हैं।

// Using reflection. 
    MethodInfo method = typeof(ClassName).GetMethod("Get"); 
    Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(UserAccess), true); 


    // Displaying output. 
    foreach (var attr in attributes) 
    { 
     if (attr is UserAccess) 
     { 
      var ua = (UserAccess)attr; 
      System.Console.WriteLine("{0}",a.userRole); 
     } 
    } 

* मैं भी एक सम्मेलन के रूप में अपने UserAccess वर्ग के लिए शब्द गुण प्रत्यय करने के लिए सुझाव देते हैं। उदाहरण के लिए, UserAccessAttribute

+0

इस पर विचार करने के लायक है .. धन्यवाद – user1791567

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