使用凭据属性来标识Webservice的用户

时间:2019-01-07 10:43:59

标签: c# web-services credentials

我正在按如下方式调用网络服务:

   public static void Execute()
        {
            var nowTS = DateTime.UtcNow.ToString("s") + "Z";
            var userNr = "XXXXXX";
            var username = "XXXXXX";
            var password = "XXXXXX";

            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(
            @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:kun=""http://tempuri.org/XXXXXXXXXXXXX"">
            <soapenv:Header>
            <wsse:Security soapenv:mustUnderstand =""1"" xmlns:wsse =""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:wsu =""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"">
            <wsse:UsernameToken xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" >
            <wsse:Username>" + username + @"</wsse:Username>
            <wsse:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">" + password + @"</wsse:Password></wsse:UsernameToken>
            <wsse:Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary""></wsse:Nonce>
            <wsu:Timestamp wsu:Id=""timestamp"">
                <wsu:Created>" + nowTS.ToString() + @"</wsu:Created>
            </wsu:Timestamp>
            </wsse:Security>
            </soapenv:Header>
            <soapenv:Body>
                <kun:kundeoverblik>
                    <kun:parameters>
                        <kun:Brugernr>" + userNr + @"</kun:Brugernr>
                    </kun:parameters>
                </kun:kundeoverblik>
            </soapenv:Body>
            </soapenv:Envelope>");




            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(response.GetResponseStream());
                    }
                }

            }
            catch (WebException webException)
            {
                WebResponse webResponse = webException.Response;
                using (Stream responseStream = webResponse.GetResponseStream())
                {
                    if (responseStream == null) { throw; }
                    StreamReader reader = new StreamReader(responseStream);
                    String text = reader.ReadToEnd();
                    throw new WebException(text, webException);
                }
            }

        }
        public static HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://XXXXXXXXXXXXXXXXXXXX");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

该呼叫工作正常,但我想使用凭据属性来标识用户。如何修改我的soapEncelopeXml,以便可以使用UseDefaultCredentials而不是使用密码和用户名?还是有可能?

我很难通过使用“添加服务引用” /“ Web参考”来使此调用正常工作,但无法使其正常工作。...因此最终得到了soapSovelopeXml解决方案。

将很感激和建设性的批评家:)

0 个答案:

没有答案