2015-10-14 214 views
0

我想使用此代碼來調用Web服務,但結果是未授權的401 - (服務內部代號)SOAP客戶端身份驗證C#401

 BasicHttpBinding myBinding1 = new BasicHttpBinding(BasicHttpSecurityMode.Transport); 
     myBinding1.Security.Mode = BasicHttpSecurityMode.Transport; 
     myBinding1.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 

     //WSHttpBinding myBinding = new WSHttpBinding(); 
     //myBinding.Security.Mode = SecurityMode.Transport; 
     //myBinding.Security.Transport.ClientCredentialType = 
     // HttpClientCredentialType.Basic; 


     EndpointAddress endpoint = new EndpointAddress("https://api.gov.uz/api/Service?ws=1"); 


     ApiControllerPortTypeClient service = new ApiControllerPortTypeClient(myBinding1, endpoint); 

     service.ClientCredentials.UserName.UserName = "ima"; 
     service.ClientCredentials.UserName.Password = "sKP8yQRstRTvV4z7"; 

     string result = service.getTasks("3824", "1", "xml"); 

下面是配置文件:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="ApiControllerBinding"> 
        <security mode="Transport" /> 
       </binding> 
       <binding name="ApiControllerBinding1" /> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="https://api.gov.uz/api/Service?ws=1" binding="basicHttpBinding" 
       bindingConfiguration="ApiControllerBinding" contract="Epigov.ApiControllerPortType" 
       name="ApiControllerPort" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

如何發送憑證?

編輯:當我在提琴手監控,HTTP請求返回200 OK。但API服務返回的XML

1401Unauthorized

也許需要自定義授權。任何建議,請幫助

下面的PHP工作

$authParams = array(
      'login' => 'ima', 
      'password' => 'sKP8yQRstRTvV4z7', 
      'cache_wsdl' => WSDL_CACHE_NONE, 
     ); 
     $client = new SoapClient('https://api.gov.uz/api/Service', $authParams); 
print_r($client->getTasks(3824, "1", 'xml')); 

編輯:

我的SOAP消息是象下面這樣:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><q1:getTasks xmlns:q1="urn:ApiControllerwsdl"><authproc_id xsi:type="xsd:integer">3824</authproc_id><page xsi:type="xsd:integer">1</page><format xsi:type="xsd:string">xml</format></q1:getTasks></s:Body></s:Envelope> 

沒有登錄名和密碼信息。

編輯:

BasicHttpBinding myBinding1 = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential); 
    myBinding1.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential; 
    myBinding1.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
+0

什麼是WS文檔說關於認證?看看他們要求什麼,並嘗試使用WCF – Luizgrs

回答

0

試試這個,改變你的憑證來規範:

var credentials = new NetworkCredential("ima", "sKP8yQRstRTvV4z7"); 

service.ClientCredentials.Windows.ClientCredential = credentials; 
+0

服務器堆棧跟蹤:System.ServiceModel.ClientCredentialsSecurityTokenManager.CreateSecurityTokenProvider(SecurityTokenRequirement tokenRequirement,Boolean disableInfoCard)。以錯誤 – Bakhodir

0
public static void CallWebService() 
{ 
    var _url = "https://api.gov.uz/api/Service?ws=1"; 
    var _action = "urn:ApiControllerwsdl#getTasks"; 

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope(); 


    HttpWebRequest webRequest = CreateWebRequest(_url, _action); 

    byte[] credentialBuffer = new UTF8Encoding().GetBytes("ima" + ":" + "sKP8yQRstRTvV4z7"); 
    webRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer); 

    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); 

    // begin async call to web request. 
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); 

    // suspend this thread until call is complete. You might want to 
    // do something usefull here like update your UI. 
    asyncResult.AsyncWaitHandle.WaitOne(); 

    // get the response from the completed web request. 
    string soapResult; 
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) 
    { 
     using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) 
     { 
      soapResult = rd.ReadToEnd(); 
     } 
     Console.Write(soapResult); 
    } 
} 

private static HttpWebRequest CreateWebRequest(string url, string action) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
    webRequest.Headers.Add("SOAPAction", "urn:ApiControllerwsdl#getTask"); 
    webRequest.ContentType = "text/xml;charset=\"utf-8\""; 
    webRequest.Accept = "text/xml"; 
    webRequest.Method = "POST"; 
    return webRequest; 
} 

private static XmlDocument CreateSoapEnvelope() 
{ 
    XmlDocument soapEnvelop = new XmlDocument(); 
    //soapEnvelop.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""?><SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns1=""urn:ApiControllerwsdl"" 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:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><SOAP-ENV:Body><ns1:getTasks><authproc_id xsi:type=""xsd:integer"">3824</authproc_id><page xsi:type=""xsd:integer"">1</page><format xsi:type=""xsd:string"">xml</format></ns1:getTasks></SOAP-ENV:Body></SOAP-ENV:Envelope>"); 
    soapEnvelop.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""?><SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns1=""urn:ApiControllerwsdl"" 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:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><SOAP-ENV:Body><ns1:getTask><id xsi:type=""xsd:string"">352275</id><format xsi:type=""xsd:string"">xml</format></ns1:getTask></SOAP-ENV:Body></SOAP-ENV:Envelope>"); 
    return soapEnvelop; 
} 

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) 
{ 
    using (Stream stream = webRequest.GetRequestStream()) 
    { 
     soapEnvelopeXml.Save(stream); 
    } 
}