gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:14 25 4
gpt4 key购买 nike

我最近一直在为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com