2010-10-20 28 views
7

प्रश्न: मेरे पास एक दस्तावेज़ प्रबंधन प्रणाली है, और मैं डेटाबेस में एक वेब-सेवा इंटरफेस बना रहा हूं।एक वेब सेवा सुरक्षित?

सबकुछ अब तक काम करता है, बस अभी यह पूरी तरह से असुरक्षित है, हर कोई इसे एक्सेस कर सकता है।

मैं पासवर्ड या निजी-सार्वजनिक कुंजी प्रमाणीकरण कैसे शामिल कर सकता हूं?

मैं केवल 'सर्वोत्तम प्रथाओं' और 'विंडोज उपयोगकर्ता' या पासपोर्ट प्रमाणीकरण का उपयोग कर सकता हूं। लेकिन मैं एक उपयोगकर्ता और पासवर्ड डेटाबेस में संग्रहीत, या बेहतर कोई RSA निजी कुंजी डेटाबेस में प्रत्येक वेब सेवा उपयोगकर्ता के लिए संग्रहीत के लिए से प्रमाणीकरण की आवश्यकता है ...

संपादित करें:
मैं उपयोग करने के लिए एक एएसपी.NET पर्यावरण में .NET Framework 2.0

उत्तर

6

समाधान MSDN और CodeProject द्वारा प्रदान की कोड के मिश्रण से एक खुद http मॉड्यूल लिखने के लिए है। एमएस बग के अपने फिक्स सहित, और फिर वेब कस्टम में यह कस्टम साबुन शीर्षलेख जोड़ें।

Imports System.Web 
Imports System.Web.Services.Protocols 


' http://msdn.microsoft.com/en-us/library/9z52by6a.aspx 
' http://msdn.microsoft.com/en-us/library/9z52by6a(VS.80).aspx 




' http://www.codeproject.com/KB/cpp/authforwebservices.aspx 


' http://aleemkhan.wordpress.com/2007/09/18/using-wse-30-for-web-service-authentication/ 
' http://www.codeproject.com/KB/WCF/CustomUserNamePassAuth2.aspx 
' http://www.codeproject.com/KB/WCF/CustomUserNamePassAuth2.aspx 
' http://www.codeproject.com/KB/webservices/WS-Security.aspx 




'Public NotInheritable Class WebServiceAuthenticationModule 
Public Class WebServiceAuthenticationModule 
    Implements System.Web.IHttpModule 

    Protected Delegate Sub WebServiceAuthenticationEventHandler(ByVal sender As [Object], ByVal e As WebServiceAuthenticationEvent) 
    Protected _eventHandler As WebServiceAuthenticationEventHandler = Nothing 



    Protected Custom Event Authenticate As WebServiceAuthenticationEventHandler 
     AddHandler(ByVal value As WebServiceAuthenticationEventHandler) 
      _eventHandler = value 
     End AddHandler 
     RemoveHandler(ByVal value As WebServiceAuthenticationEventHandler) 
      _eventHandler = value 
     End RemoveHandler 
     RaiseEvent(ByVal sender As Object, 
       ByVal e As WebServiceAuthenticationEvent) 
     End RaiseEvent 
    End Event 


    Protected app As HttpApplication 


    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init 
     app = context 

     context.Context.Response.Write("<h1>Test</h1>") 

     AddHandler app.AuthenticateRequest, AddressOf Me.OnEnter 
    End Sub 


    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose 
     ' add clean-up code here if required 
    End Sub 


    Protected Sub OnAuthenticate(ByVal e As WebServiceAuthenticationEvent) 
     If _eventHandler Is Nothing Then 
      Return 
     End If 
     _eventHandler(Me, e) 
     If Not (e.User Is Nothing) Then 
      e.Context.User = e.Principal 
     End If 

    End Sub 'OnAuthenticate 


    Public ReadOnly Property ModuleName() As String 
     Get 
      Return "WebServiceAuthentication" 
     End Get 
    End Property 


    Sub OnEnter(ByVal [source] As [Object], ByVal eventArgs As EventArgs) 
     'Dim app As HttpApplication = CType([source], HttpApplication) 
     'app = CType([source], HttpApplication) 
     Dim context As HttpContext = app.Context 
     Dim HttpStream As System.IO.Stream = context.Request.InputStream 

     ' Save the current position of stream. 
     Dim posStream As Long = HttpStream.Position 

     ' If the request contains an HTTP_SOAPACTION 
     ' header, look at this message. 

     'For Each str As String In context.Request.ServerVariables.AllKeys 

     'If context.Request.ServerVariables(Str) IsNot Nothing Then 
     'context.Response.Write("<h1>" + Str() + "= " + context.Request.ServerVariables(Str) + "</h1>") 
     'End If 
     'Next 
     If context.Request.ServerVariables("HTTP_SOAPACTION") Is Nothing Then 
      'context.Response.End() 
      Return 
      'Else 
      'MsgBox(New System.IO.StreamReader(context.Request.InputStream).ReadToEnd()) 
     End If 


     ' Load the body of the HTTP message 
     ' into an XML document. 
     Dim dom As New System.Xml.XmlDocument() 
     Dim soapUser As String 
     Dim soapPassword As String 

     Try 
      dom.Load(HttpStream) 

      'dom.Save("C:\Users\Administrator\Desktop\SoapRequest.xml") 
      ' Reset the stream position. 
      HttpStream.Position = posStream 

      ' Bind to the Authentication header. 
      soapUser = dom.GetElementsByTagName("Username").Item(0).InnerText 
      soapPassword = dom.GetElementsByTagName("Password").Item(0).InnerText 
     Catch e As Exception 
      ' Reset the position of stream. 
      HttpStream.Position = posStream 

      ' Throw a SOAP exception. 
      Dim name As New System.Xml.XmlQualifiedName("Load") 
      Dim ssoapException As New SoapException("Unable to read SOAP request", name, e) 
      context.Response.StatusCode = System.Net.HttpStatusCode.Unauthorized 
      context.Response.StatusDescription = "Access denied." 

      ' context.Response.Write(ssoapException.ToString()) 
      'Dim x As New System.Xml.Serialization.XmlSerializer(GetType(SoapException)) 
      'context.Response.ContentType = "text/xml" 
      'x.Serialize(context.Response.OutputStream, ssoapException) 


      'Throw ssoapException 

      context.Response.End() 
     End Try 

     ' Raise the custom global.asax event. 
     OnAuthenticate(New WebServiceAuthenticationEvent(context, soapUser, soapPassword)) 
     Return 
    End Sub 'OnEnter 


End Class ' WebServiceAuthenticationModule 
2

यदि आप डब्ल्यूसीएफ के साथ काम कर रहे हैं, तो X509 प्रमाणपत्रों का उपयोग करके सुरक्षा को लागू करने का एक आसान तरीका है। सुरक्षा मोड 'संदेश' और क्लाइंट क्रेडेंशियल टाइप 'उपयोगकर्ता नाम' के साथ बाध्यकारी कार्यान्वित करना इस सुरक्षा को स्वचालित तरीके से गारंटी देना संभव है।

सत्यापन एक वर्ग के माध्यम से किया जा सकता है जो एक विधि सत्यापन को ओवरराइड करता है।

http://msdn.microsoft.com/en-us/library/aa702565.aspx

+1

+1, लेकिन दुर्भाग्यवश, मैं फ्रेमवर्क 2.0 के साथ एएसपी.NET तक सीमित हूं, इसलिए कोई डब्ल्यूसीएफ नहीं है। –

+0

ओपीएस, मैं उस मामले में खो गया हूं, मैं समझता हूं कि परिवहन स्तर पर संचार X509 प्रमाणपत्रों के साथ भी किया जा सकता है। हो सकता है कि SQL सर्वर के विरुद्ध सत्यापन प्रक्रिया अधिक 'मैन्युअल' होगी। वैसे भी आप यहां अधिक जानकारी प्राप्त कर सकते हैं: http://msdn.microsoft.com/en-us/library/ms996415.aspx – IoChaos

1

इससे पहले कि आप अपनी खुद की प्रमाणीकरण रोल, आप Web Services Enhancements (WSE) 2.0 SP3 for Microsoft .NET पर एक नजर है करने के लिए चाहते हो सकता है:

<SoapHeader("Authentication", Required:=True)> 

इस मॉड्यूल है। यह .net के लिए WS-Security विनिर्देश का कार्यान्वयन है। अधिक लिंक के लिए

google wse 2.0 या WS-Security

+0

डब्लूएसई से लिंक टूटा हुआ है - क्या लेखक कृपया सही कर सकते हैं? –

5

यदि आप अभी भी एएसपी.नेट एसओएपी वेब सेवा का उपयोग कर रहे हैं तो आपकी आवश्यकताओं को फिट करने का सबसे आसान तरीका आईएमओ एक सदस्यता डीबी के साथ एएसपी.नेट फॉर्म प्रमाणीकरण का उपयोग करना है। यदि आप ताजा शुरू कर रहे हैं तो मैं डब्ल्यूसीएफ के साथ जाने की सिफारिश करता हूं - अगर आप यह नहीं कर सकते/नहीं करेंगे कि यह पोस्ट "क्लासिक" एएसपी.नेट एसओएपी वेब सेवाओं पर लागू होता है। वैसे ही जैसे आप किसी भी अन्य वेब साइट के लिए होगा

  1. कॉन्फ़िगर यह लेकिन यह सेट हर किसी के लिए उपयोग की अनुमति:

    एक वेब सेवा के लिए फार्म प्रमाणीकरण को जोड़ने के लिए

    <authorization> 
        <allow users="*"/> 
    </authorization> 
    
  2. लॉगिन/लॉगआउट विधियों को कार्यान्वित करें और लॉगिन विधि में प्रमाणीकरण टिकट जारी करें। वेब सेवा के लिए और अनुरोध जारी किए गए प्रमाणीकरण टिकट का उपयोग कर सकते हैं।

  3. अन्य सभी वेब तरीकों आप आप तो साथ

    [PrincipalPermission (SecurityAction.Demand, प्रमाणीकृत = true)] को सजाने कर सकते हैं की रक्षा करना चाहते हैं

इन विधियों अब एक सुरक्षा अपवाद अगर फेंक होगा एक ग्राहक प्रमाणीकृत नहीं है। एक संरक्षित विधि के लिए

उदाहरण:

[PrincipalPermission(SecurityAction.Demand, Authenticated = true)] 
[WebMethod(Description = "Your protected method")] 
public string Foo() 
{ 
    return "bar"; 
} 

लॉग इन विधि के लिए उदाहरण:

[WebMethod(Description = "Login to start a session")] 
public bool Login(string userName, string password) 
{ 
    if (!Membership.Provider.ValidateUser(userName, password)) 
     return false; 

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
      1, 
      userName, 
      DateTime.Now, 
      DateTime.Now.AddMinutes(500), 
      false, 
      FormsAuthentication.FormsCookiePath);// Path cookie valid for 

    // Encrypt the cookie using the machine key for secure transport 
    string hash = FormsAuthentication.Encrypt(ticket); 
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, // Name of auth cookie 
             hash); // Hashed ticket 

    // Set the cookie's expiration time to the tickets expiration time 
    if (ticket.IsPersistent) 
     cookie.Expires = ticket.Expiration; 

    // Add the cookie to the list for outgoing response 
    if(HttpContext.Current !=null) 
     HttpContext.Current.Response.Cookies.Add(cookie); 

    FormsAuthentication.SetAuthCookie(userName, true); 
    return true; 
} 
+0

और फिर जब भी आप वेब सेवा का उपयोग करना चाहते हैं, ब्लैकबॉक्स ट्रांसमिशन सिस्टम के साथ, और क्लाइंट या सर्वर क्रैश होने पर अपरिभाषित व्यवहार के साथ आपको लॉगिन और लॉगआउट करना होगा। और यदि आप प्रिंसिपलप्रमिशन को एक ही विधि में जोड़ना भूल जाते हैं ... फिर भी, जो भी गंभीर होना चाहता है उसे डिफ़ॉल्ट रूप से एक्सेस से इनकार करना चाहिए, और केवल लॉगिन पैरामीटर को सर्विस पैरामीटर के रूप में लेना चाहिए, न कि विधि के रूप में। –

+0

यह नियमित यूआई फॉर्म प्रमाणीकरण से अलग नहीं है, फिर भी खुद को रोल करने से संबंधित नुकसान के साथ सही होना आसान है। इसके अलावा आपको मुफ्त में भूमिकाएं/सदस्यता सुविधाएं मिलती हैं। हालांकि डिफ़ॉल्ट रूप से सुरक्षित पर लिया गया बिंदु। – BrokenGlass

0

अपने WS सोप प्रोटोकॉल के माध्यम से भस्म हो जा रहा है, तो आप लागू कर सकते हैं एसओएपी हैडर के माध्यम से सुरक्षा:

using System.Web.Services; 
using System.Web.Services.Protocols; 

namespace Domain.WS 
{ 
    [Serializable] 
    public class SoapWSHeader : System.Web.Services.Protocols.SoapHeader, ISoapWSHeader 
    { 
     public string UserId { get; set; } 
     public string ServiceKey { get; set; } 
     public ApplicationCode ApplicationCode { get; set; }   
    }  

    [WebService(Namespace = "http://domain.some.unique/")]   
    public class MyServices : System.Web.Services.WebService 
    { 
     public SoapWSHeader WSHeader; 
     private ServicesLogicContext _logicServices; 

     public MyServices() { _logicServices = new ServicesLogicContext(new LogicInfo() {...}); } 

     [WebMethod, SoapHeader("WSHeader", Direction = SoapHeaderDirection.InOut)] 
     public Result WSMethod1(Int32 idSuperior) 
     { 
      _logicServices.ThrowIfNotAuthenticate(WSHeader); 
      return _logicServices.WSMethod1(idSuperior) as Result; 
     } 
    } 
} 

namespace Domain.Logic 
{ 
    [Serializable]  
    public class ServicesLogicContext : ServicesLogicContextBase 
    { 
     protected ISoapWSHeader SoapWSHeader { get; set; } 
     public ServicesLogicContext(LogicInfo info) : base(info) {} 

     public IResult WSMethod1(Int32 idSuperior) 
     { 
      IResult result = null; 
      //-- method implementation here... 
      return result; 
     } 

     public void ThrowIfNotAuthenticate(ISoapWSHeader soapWSHeader) { 
      this.SoapWSHeader = soapWSHeader; 
      if (SoapWSHeader != null) 
      { 
       if (!ValidateCredentials(soapWSHeader)) 
       { 
        throw new System.Security.SecurityException(Resources.ValidationErrorWrongCredentials); 
       } 
      } 
      else { throw new System.Security.SecurityException(Resources.ValidationErrorWrongWSHeader); } 
     } 
     private bool ValidateCredentials(ISoapWSHeader soapWSHeader) { 
      return (SoapWSHeader.UserId.Equals("USER_ID") && SoapWSHeader.ServiceKey.Equals("PSW_1")); 
     } 
    } 
} 

नोट: यह कोड पूर्ण नहीं हुआ है, यह केवल एसओएपी हैडर का उपयोग करने के तरीके पर मुख्य पहलुओं को दर्शाता है।

+0

यदि आपने ध्यान दिया था, तो आपको एहसास हुआ होगा कि यह वही है जो मैंने किया है। बस मैंने HTTP मॉड्यूल को कहने के लिए प्रत्येक वेबमाइंड में शामिल होने से प्रमाणीकरण को स्थानांतरित कर दिया है, जो बीएस है। अतिरिक्त बोनस के साथ कि यह शीर्षलेख के बिना प्रत्येक SOAP कॉल तक पहुंच से इनकार करता है। –

+0

ठीक है, इसका मतलब है कि आपने अपनी समस्या हल कर ली है। नमस्ते, हम आपके डब्ल्यूएस को सुरक्षित करने के लिए उस तकनीक को लागू कर रहे हैं, आपके आवेदन के आधार पर आप एचटीटीपीएस के माध्यम से कुछ प्रमाण पत्र का उपयोग कर सकते हैं। – ArBR

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