肥皂客户端身份验证c#401

时间:2015-10-14 16:37:26

标签: c# soap

我正在尝试使用此代码来调用Web服务,但结果是未经授权的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>

如何发送凭据?

编辑:当我在Fiddler中监视时,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'));

编辑:

我的肥皂消息如下:

<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;

2 个答案:

答案 0 :(得分:0)

尝试此操作,将凭据规格更改为:

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

service.ClientCredentials.Windows.ClientCredential = credentials;

答案 1 :(得分: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);
    }
}