2012-02-07 22 views
5

मैं यहां लिखता हूं क्योंकि मैं एक PHP webservice क्लाइंट को C# (VS2010) में बदलने के लिए काम कर रहा हूं।वेब सेवा - सी # क्लाइंट और अपाचे सर्वर मूल प्रमाणीकरण - त्रुटि HTTP 400 खराब अनुरोध

प्रोजेक्ट फ्रेमवर्क 4 में हैं लेकिन वेब सेवा को वीएस -2010 में फ्रेमवर्क 2 (डब्ल्यूएसडीएल के साथ वेब संदर्भ) के साथ संगत वेब सेवा की तरह जोड़ा गया था।

असल में मैं केवल शास्त्रीय webservice (एक Helloworld परीक्षण) पर काम कर रहा हूं।

क्लाइंट सी # के साथ मुझे कोई समस्या नहीं है मैं वेब सेवा में एक टेक्स्ट भेजता हूं और वेब सेवा टेक्स्ट को पुनः प्राप्त करके प्रतिक्रिया देती है। मुझे अपाचे सर्वर में कोई त्रुटि नहीं है।

मैं .htaccess अपाचे सर्वर में बुनियादी authentification सक्रिय करते हैं (वेब ​​सेवा सर्वर की मेजबानी), इसलिए में ग्राहक सी # मैं इस त्रुटि है:

अपवाद: WebException बुरा राज्य HTTP 400 के साथ अनुरोध: गलत अनुरोध ।

[Thu Feb 02 23:52:06 2012] [error] [client XX.XX.XX.XX] Invalid URI in request 
<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:tns="https://www.domaine.com/api/helloworld/webservices.php?wsdl" 
xmlns:types="https://www.domaine.com/api/helloworld/webservices.php?wsdl/encodedTypes" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org 
/2001/XMLSchema"> 
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
<tns:bonjour><prenom xsi:type="xsd:string">Toto</prenom></tns:bonjour> 
</soap:Body> 
</soap:Envelope> 
POST /api/helloworld/webservices.php HTTP/1.1 

:

अपाचे त्रुटि लॉग में मैं इस लाइन को देखते हैं।

मुझे यकीन है कि ऐसे प्रमाण पत्र हैं जो काम नहीं कर रहे हैं क्योंकि जब मैं झूठे उपयोगकर्ता और पासवर्ड के साथ प्रयास करता हूं तो मेरे पास हमेशा 400 खराब अनुरोध होता है।

सी # क्लाइंट का कोड हैं: // हम webservice वेब सेवा mywebservice = new WebService() बनाते हैं;

// We define BASIC Authentication    
CredentialCache myCredentials = new CredentialCache();   
NetworkCredential netCred = new NetworkCredential("User1", "Pass1");  
myCredentials.Add(new Uri(mywebservice.Url), "Basic", netCred); 
myCredentials.PreAuthenticate = true; 
mywebservice.Credentials = myCredentials; 

// We define the UserAgent 
mywebservice.UserAgent = ".NET Framework"; 

// We call the function of webservice 
string retour = mywebservice.bonjour("Tata"); 

// We show the return 
MessageBox.Show(retour, "Retour de l'API", MessageBoxButtons.OK, MessageBoxIcon.Information); 

.htaccess: # निर्देशिका के लिए मानचित्र जो URL के लिए निर्देशिका लिस्टिंग नहीं दिखाएं। विकल्प- इंडेक्स

# Se logue avec un compte AD 
AuthType Basic 
AuthBasicProvider ldap 
AuthName "Acces au webservice" 
AuthLDAPURL ldap://127.0.0.1:389/ou=clients,dc=domaine,dc=com?mail 
Require valid-user 

अगर मैं .htaccess फाइल सब कुछ यह काम कर रहा है ... लेकिन प्रमाणीकरण :(

बिना मैं भी पोस्ट अनुरोध और .htaccess (काम नहीं कर रहा) के साथ उत्तर हटाया:।

POST https://www.domaine.com/api/login_ovh_pnp/webservices.php HTTP/1.1 
User-Agent: .NET Framework 
VsDebuggerCausalityData: uIDPo9/Tb/xbJxtKvSNra7boyJsAAAAAQcQin1OhXXXXXXXXXXX/KqROJW0w1FhAcrrhI1sGQACQAA 
Content-Type: text/xml; charset=utf-8 
SOAPAction: "https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl#bonjour" 
Authorization: Basic bHZlaXJtYW5AYXF1aWxhLWXXXXXXXXXXXbmcuZnI6ZHluYXRlcmE3OA== 
Host: www.domaine.com 
Content-Length: 623 
Expect: 100-continue 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:tns="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl" 
xmlns:types="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl 
/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
<tns:bonjour> 
<prenom xsi:type="xsd:string">Tata</prenom> 
</tns:bonjour> 
</soap:Body>  
</soap:Envelope> 

===================================== 
===================================== 

HTTP/1.1 400 Bad Request 
Date: Mon, 06 Feb 2012 13:40:32 GMT 
Server: Apache 
Vary: Accept-Encoding 
Content-Length: 226 
Connection: close 
Content-Type: text/html; charset=iso-8859-1 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>400 Bad Request</title> 
</head><body> 
<h1>Bad Request</h1> 
<p>Your browser sent a request that this server could not understand.<br /> 
</p> 
</body></html> 

और खत्म करने के लिए .htaccess बिना अनुरोध:

POST https://www.domaine.com/api/login_ovh_pnp/webservices.php HTTP/1.1 
User-Agent: .NET Framework 
VsDebuggerCausalityData:  
uIDPo4r/MO3TgmdMkQiWHfkXoWwAAAAA1krYOM0NBkS9vLzFQe3Vmln8F3GXClFNrTWXL/L622YACQAA 
Content-Type: text/xml; charset=utf-8 
SOAPAction: "https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl#bonjour" 
Host: www.domaine.com 
Content-Length: 623 
Expect: 100-continue 
Connection: Keep-Alive 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:tns="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl" 
xmlns:types="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl 
/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
<tns:bonjour> 
<prenom xsi:type="xsd:string">Tata</prenom> 
</tns:bonjour> 
</soap:Body></soap:Envelope> 

===================================== 
===================================== 

HTTP/1.1 200 OK 
Date: Mon, 06 Feb 2012 13:51:31 GMT 
Server: Apache 
X-SOAP-Server: NuSOAP/0.9.5 (1.123) 
Content-Length: 575 
Vary: Accept-Encoding 
Keep-Alive: timeout=15, max=100 
Connection: Keep-Alive 
Content-Type: text/xml; charset=ISO-8859-1 

<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> 
<SOAP-ENV:Body> 
<ns1:bonjourResponse xmlns:ns1="https://supervision.dynatera.net/api/login_ovh_pnp/webservices.php?wsdl"> 
<return xsi:type="xsd:string">Bonjour Tata</return></ns1:bonjourResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

इस जटिल समस्या पर आपकी मदद के लिए बहुत बहुत धन्यवाद क्योंकि मैंने इस समस्या को ठीक करने के लिए कुछ भी नहीं पाया है।

+0

क्या आप सोपुई के साथ सुरक्षित सेवा को कॉल कर सकते हैं? – davidfrancis

+0

पोस्ट उपयोगकर्ता/पासवर्ड को हटाने के लिए संपादित –

उत्तर

0

साख अपने कोड अनुरोध में भेजा क्रेडेंशियल्स मेल नहीं खाता में सेट

Authorization: Basic bHZlaXJtYW5AYXF1aWxxxxxx1bHRpbmcuZnI6ZHluYXRlcmE3OA== 

बुनियादी प्रमाणीकरण के लिए आपका कोड सेट अगले क्रेडेंशियल:

// We define BASIC Authentication    
CredentialCache myCredentials = new CredentialCache();   
NetworkCredential netCred = new NetworkCredential("User1", "Pass1"); 

लेकिन प्राधिकरण अनुरोध टोकन के अंदर क्रेडेंशियल्स हैं:

"[email protected]:dynXXXXXXX" 

(मैं आपको अपनी पोस्ट संपादित करने और बेस 64 को हटाने की सलाह देता हूं प्रमाणीकरण टोकन, इसके अलावा एक अच्छा विचार है यथाशीघ्र < < किया अपने नेटवर्क पासवर्ड)

4

मैं भी इस समस्या को मिला है बदलने के लिए ... जब आप NetworkCredentials यह HTTP शीर्ष लेख में भेजा नहीं प्राप्त करता है, और इस तरह परिभाषित नहीं 'प्राधिकरण' शीर्षक ...

क्या मैंने किया था और यह मेरे लिए काम किया था:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.ServiceModel.Dispatcher; 
using System.ServiceModel.Channels; 

namespace BasicAuth 
{ 
    public sealed class MessageHttpHeaderInspector : IClientMessageInspector 
    { 
     private string userName; 
     private string password; 

     #region Constructor 

     public MessageHttpHeaderInspector(string userName, string password) 
     { 
      this.userName = userName; 
      this.password = password; 
     } 

     #endregion 

     #region IClientMessageInspector Members 

     public void AfterReceiveReply(ref Message reply, object correlationState) 
     { 
      //throw new NotImplementedException(); 
     } 

     public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel) 
     { 
      HttpRequestMessageProperty httpRequest; 

      if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) 
      { 
       httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; 
      } 
      else 
      { 
       httpRequest = new HttpRequestMessageProperty(); 
       request.Properties.Add(HttpRequestMessageProperty.Name, httpRequest); 
      } 

      if (httpRequest != null) 
      { 
       string credentials = this.CreateBasicAuthenticationCredentials(this.userName, this.password); 
       httpRequest.Headers.Add(System.Net.HttpRequestHeader.Authorization, credentials); 
      } 

      return request; 
     } 

     #endregion 

     #region Private Worker Methods 

     private string CreateBasicAuthenticationCredentials(string userName, string password) 
     { 
      string returnValue = string.Empty; 
      string base64UsernamePassword = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", userName, password))); 

      returnValue = String.Format("Basic {0}", base64UsernamePassword); 

      return returnValue; 
     } 

     #endregion 
    } 
} 
:

मैं एक वर्ग कि IClientMessageInspector लागू करता है बनाया है और BeforeSendRequest तरीका लागू

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.ServiceModel; 
using System.ServiceModel.Description; 
using System.ServiceModel.Dispatcher; 

namespace BasicAuth 
{ 
    public sealed class CustomEndpointCallBehavior : IEndpointBehavior 
    { 
     private string userName;  
     private string password; 

     #region Constructor 

     public CustomEndpointCallBehavior(string userName, string password) 
     { 
      this.userName = userName;  
      this.password = password; 
     } 

     #endregion 

     #region IEndpointBehavior Members 

     public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
     { 
      //throw new NotImplementedException(); 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
      clientRuntime.MessageInspectors.Add(new MessageHttpHeaderInspector(this.userName, this.password)); 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
      //throw new NotImplementedException(); 
     } 

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

     #endregion 
    } 
} 

तो फिर तुम प्रॉक्सी को यह व्यवहार लागू करने की आवश्यकता:

mywebservice.Endpoint.Behaviors.Add(new CustomEndpointCallBehavior("User1", "Pass1")); 

निम्नलिखित पर एक नज़र डालें:

तो मैं एक वर्ग कि IEndpointBehavior लागू करता है और ApplyClientBehavior विधि को लागू करता है बनाया

इस चाल करना चाहिए ... यह आप के लिए काम करता है आशा है!

+1

धन्यवाद दस लाख। यह बिना किसी समस्या के काम किया। –

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