c# - 使用 C# 的基于 WSSE 的服务的客户端

标签 c# web-services basic-authentication wsse

我最近一直在为 WSS 服务编写客户端。它根据其文档使用 usernametoken 配置文件。我已经写了这样的东西作为代码来传达它。但是由于不允许匿名身份验证,我总是面临服务未经授权的错误。
我该如何解决这个问题?

示例代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Execute();
        }
        public  void Execute()
        {
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Header>
<wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<wsse:UsernameToken>
<wsse:Username>something</wsse:Username>
<wsse:Password>something</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
");
            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
        }
        public HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://127.0.0.1:5434");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }
    }
}

感谢任何帮助。

最佳答案

有两种解决方案可以实现 WCF 的 UsernameToken Profile 支持:

那么,为什么不使用 WCF 而不是发出原始请求呢?

关于c# - 使用 C# 的基于 WSSE 的服务的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545084/

相关文章:

c# - 如何使用平台调用编码 void*

web-services - 如何通过主干验证和使用外部 api?

jquery - 使用 jQuery 调用远程服务器上的 Web 服务

python - HTTP 基本身份验证在 python 脚本中失败

HTTP 规范 : Proxy-Authorization and Authorization headers

c# - 在 C# 中是否可以使用同名的公共(public) getter 和私有(private) setter?

c# - 解析格式为 "2013-Jan-31"的日期时间会抛出错误

c# - 如何使用 DateTime.Now 函数

.net - 从 .NET 3.5 WCF Web 服务 (REST) 返回 JSON 和 XML 格式

java - 我可以从 Axis2 Web 服务调用 Hadoop API 吗?