- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
为此,我在网上找遍了。我一直在做这件事,但我尝试使用其 Web 服务的供应商拒绝正式支持 WCF 作为一种使用方法。
我不是 Web 服务专家,所以我会尽我所能在最初的帖子中记录和解释,但无论如何,如果您需要更多信息,请索取更多信息,希望我能够提供任何信息是必要的。
服务
在我的公司,我们使用公开服务的供应商应用程序。该应用程序是用 java 编写的,看起来 wsdl 是使用 Apache Axis 1.2 创建的。
代码
我的遗留代码使用 WSE 3.0。特别是,它使用了在末尾自动添加“WSE”的代理类。这使我可以使用更简单的身份验证方案(这是我让它工作的唯一方法)。我不需要使用证书。我使用 SecurityPolicyAssertion
的派生词,并将其包装在 Policy
对象中,该对象传递给客户端类的 SetPolicy
方法。这是创建客户端的工作实例所需要做的所有事情:
MyWebServiceWse api = new MyWebServiceWse();
api.Url = myUrl;
api.SetPolicy(new Policy(new MyDerivedSecurityAssertion(user, pass)));
我的 WCF 默认开箱即用代码(使用服务引用生成)不接受凭据,因此我知道马上就会出现问题。我在网上阅读了各种关于在我的 app.config
中使用不同的 security
或绑定(bind)设置的内容,但没有任何内容完全有效。经过大量修改后,我最常见的错误是 WSDoAllReceiver: Request does not contain required Security header
。
这是 app.config。或许我们可以先告诉我这里应该更改哪些内容以促进凭据的通过——同样,我在网上看到了不同的意见。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MySoapBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://xyz:12345/services/MyService"
binding="basicHttpBinding" bindingConfiguration="MySoapBinding"
contract="MyNS.MyService" name="MyService" />
</client>
</system.serviceModel>
</configuration>
我更改了一些属性以模糊我们正在使用的特定服务(公司政策等)。
这是到目前为止的示例 C# 代码(在控制台应用程序中测试):
MyClient client = new MyClient();
client.listMethod();
更新
阅读这篇 SO 帖子:wcf security . . . .
我已经相应地更新了我的 app.config,现在正在代码中传递用户名和密码。我仍然收到同样的错误:
WSDoAllReceiver: Request does not contain required Security header
20120517 更新
一个成功的请求(来自 WSE3):
<soap:Header>
<wsa:Action>
</wsa:Action>
<wsa:MessageID>urn:uuid:cb739422-c077-4eec-8cb2-686837b76878</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:To>http://removed-for-security</wsa:To>
<wsse:Security soap:mustUnderstand="1">
<wsu:Timestamp wsu:Id="Timestamp-e13feaf9-33d9-47bf-ab5b-60b4611eb81a">
<wsu:Created>2012-05-17T11:25:41Z</wsu:Created>
<wsu:Expires>2012-05-17T11:30:41Z</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-00c26e1a-3b3b-400f-a99a-3aa54cf8c8ff">
<wsse:Username>change-to-protect-the-innocent</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">nice-try</wsse:Password>
<wsse:Nonce>KJMvUuWF2eO2uIJCuxJC4A==</wsse:Nonce>
<wsu:Created>2012-05-17T11:25:41Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<listChannels xmlns="http://removed-for-security">
<rowfrom>0</rowfrom>
<rowto>10</rowto>
</listChannels>
</soap:Body>
</soap:Envelope>
正在获取 WCF 跟踪——将很快添加。
20120517 更新 2
这是来自 WCF 的信封:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"></Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<listChannels xmlns="http://removed-for-security">
<rowfrom>1</rowfrom>
<rowto>2147483647</rowto>
</listChannels>
</s:Body>
</s:Envelope>
20120518 更新我已经尝试在 Mike Miller 在评论中链接到的帖子中实现解决方案。现在我收到以下错误(没有消息最终被发送,因为该方案有问题):
The provided URI scheme 'http' is invalid; expected 'https'.
如果有人想问,是的,我需要通过 http 发送,是的,我知道凭据是作为未加密的字符串发送的:-)
最佳答案
您需要通过 wcf ootb 不支持的 http 传输发送用户名 token 。此外,您的 token 使用 nonce/created,这也不是 ootb。你有两个选择:
这oss project将 nonce/created 添加到用户名 token 。这个oss project添加通过 http 发送用户名的功能。您需要将这两个项目组合在一起。
ws-security 通常被认为很复杂,但您可以以最简单的形式(用户名)使用它。最简单的方法是一起取消所有 wcf 安全设置,并在 message inspector 中自己创建整个安全 header 。 !如您所见,大多数 header 只是静态 xml 节点,并且大多数值都非常清楚(您知道用户名)。唯一棘手的两个是 nonce 和时间戳,您可以在此 oss project 中查看如何操作(每行一行)。此选项有一个变体可能更容易 - 使用 CUB毕竟并实现 custom encoder这插入了 timestmpa/nonce。我会选择后者,但我有偏见,因为我开发了 CUB...
还有 ws-addressing header ,您可以在自定义编码“messageVersion”属性上进行配置。我无法说出确切的值,因为您省略了带有 wsa 前缀定义的信封 header 。
如果您想私下寻求帮助(因为您似乎有安全限制),请务必通过 my blog 给我发一封电子邮件.
编辑:我已经为您实现了它。请按照以下步骤操作:
下载 cub并让自己熟悉它(不是内部结构,只是根据博客文章如何使用它)
将对 System.Runtime.Serialization.dll 的引用添加到项目 ClearUsernameBinding
向该项目添加一个新文件:UsernameExEncoder.cs。粘贴此内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
namespace Webservices20.BindingExtensions
{
class UsernameExEncoderBindingElement : MessageEncodingBindingElement
{
MessageEncodingBindingElement inner;
public UsernameExEncoderBindingElement(MessageEncodingBindingElement inner)
{
this.inner = inner;
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
context.BindingParameters.Add(this);
var res = base.BuildChannelFactory<TChannel>(context);
return res;
}
public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
var res = base.CanBuildChannelFactory<TChannel>(context);
return res;
}
public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new UsernameExEncoderFactory(this.inner.CreateMessageEncoderFactory());
}
public override MessageVersion MessageVersion
{
get
{
return this.inner.MessageVersion;
}
set
{
this.inner.MessageVersion = value;
}
}
public override BindingElement Clone()
{
var c = (MessageEncodingBindingElement)this.inner.Clone();
var res = new UsernameExEncoderBindingElement(c);
return res;
}
public override T GetProperty<T>(BindingContext context)
{
var res = this.inner.GetProperty<T>(context);
return res;
}
}
class UsernameExEncoderFactory : MessageEncoderFactory
{
MessageEncoderFactory inner;
public UsernameExEncoderFactory(MessageEncoderFactory inner)
{
this.inner = inner;
}
public override MessageEncoder Encoder
{
get { return new UsernameExEncoder(inner.Encoder); }
}
public override MessageVersion MessageVersion
{
get { return this.inner.MessageVersion; }
}
}
class UsernameExEncoder : MessageEncoder
{
MessageEncoder inner;
public override T GetProperty<T>()
{
return inner.GetProperty<T>();
}
public UsernameExEncoder(MessageEncoder inner)
{
this.inner = inner;
}
public override string ContentType
{
get { return this.inner.ContentType; }
}
public override string MediaType
{
get { return this.inner.MediaType; }
}
public override MessageVersion MessageVersion
{
get { return this.inner.MessageVersion; }
}
public override bool IsContentTypeSupported(string contentType)
{
return this.inner.IsContentTypeSupported(contentType);
}
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
return this.inner.ReadMessage(buffer, bufferManager, contentType);
}
public override Message ReadMessage(System.IO.Stream stream, int maxSizeOfHeaders, string contentType)
{
return this.inner.ReadMessage(stream, maxSizeOfHeaders, contentType);
}
public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
//load the message to dom
var mem = new MemoryStream();
var x = XmlWriter.Create(mem);
message.WriteMessage(x);
x.Flush();
mem.Flush();
mem.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(mem);
//add the missing elements
var token = doc.SelectSingleNode("//*[local-name(.)='UsernameToken']");
var created = doc.CreateElement("Created", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
var nonce = doc.CreateElement("Nonce", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
token.AppendChild(created);
token.AppendChild(nonce);
//set nonce value
byte[] nonce_bytes = new byte[16];
RandomNumberGenerator rndGenerator = new RNGCryptoServiceProvider();
rndGenerator.GetBytes(nonce_bytes);
nonce.InnerText = Convert.ToBase64String(nonce_bytes);
//set create value
created.InnerText = XmlConvert.ToString(DateTime.Now.ToUniversalTime(), "yyyy-MM-ddTHH:mm:ssZ");
//create a new message
var r = XmlReader.Create(new StringReader(doc.OuterXml));
var newMsg = Message.CreateMessage(message.Version, message.Headers.Action, r);
return this.inner.WriteMessage(newMsg, maxMessageSize, bufferManager, messageOffset);
}
public override void WriteMessage(Message message, System.IO.Stream stream)
{
this.inner.WriteMessage(message, stream);
}
}
}
在文件 ClearUsernameBinding.cs 中替换为:
res.Add(new TextMessageEncodingBindingElement() { MessageVersion = this.messageVersion});
用这个:
var textEncoder = new TextMessageEncodingBindingElement() { MessageVersion = this.messageVersion };
res.Add(new UsernameExEncoderBindingElement(textEncoder));
在 app.config 的项目 TestClient 中,绑定(bind)元素上有一个 messageVersion 属性。您尚未发布信封的根目录,因此我无法确定,但您可能需要将其设置为 Soap11WSAddressingAugust2004 或 Soap11WSAddressing10(或其中之一改为使用 Soap12)。
祝你好运!
关于c# - (尝试)从 WSE 3.0 迁移到 WCF 以获取客户端代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10589561/
WCF 服务、WCF RIA 服务和 WCF 数据服务之间有什么区别? 最佳答案 WCF 是一般服务的通信基础设施。 WCF RIA 服务自动生成客户端和服务器代理对象以方便应用程序开发,并依赖 WC
我想在我的 WPF 项目中使用 WCF 服务 (.svc)。, 我正在尝试创建一个服务。但在 Visual Studio 中,我们有“WCF 服务库”和“WCF 服务应用程序”。我两个都试了。 当我们
我正在开发 WCF Web 服务,并使用 WCF 服务应用程序模板来执行此操作。 创建“WCF 服务应用程序”是否满足此要求?与 WCF 服务应用程序相比,创建 WCF 服务库有哪些优势? 最佳答案
我是 WCF 的新手,对 Web 服务进行编码的经验有限。 在工作中,所有面向网络服务的事物都被要求使用 WCF。我需要做的工作涉及查询一个非 WCF Web 服务,该服务显然是用 Java 构建的,
我有一个数据契约(Contract)说用户。它是可序列化的并且可以通过网络传输。我想要一个操作契约(Contract) SaveUser()。我可以将 SaveUser(User user) 作为运营
我一直在开发一个使用 WCF 访问服务器端逻辑和数据库的 WPF 应用程序。 我从一个 WCF 客户端代理对象开始,我反复使用它来调用服务器上的方法。使用代理一段时间后,服务器最终会抛出异常: Sys
不要添加关于不同 WCF 堆栈的另一篇 SO 帖子,但我想在浪费更多开发时间之前确保我朝着正确的方向前进...... 我的场景 - 我们公司有许多 Web 应用程序,它们都访问同一系列的数据库。所有应
我是WCF技术的新手,我想知道RESTful WCF服务和普通WCF服务有什么区别。 RESTful 服务相对于普通 WCF 服务有哪些优势? 谢谢。 最佳答案 REST服务基于HTTP协议(prot
我正在构建的应用程序公开了多个 WCF 服务(A、B)。在内部,它消耗了在我们的内部网络(X、Y)上运行的其他几个 WCF 服务。 使用 WCF 消息日志记录,我希望仅记录我们的服务 A、B 与调用它
我们需要从另一个 WCF 服务调用 WCF 服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置是: 控制台应用程序 -> WCF 服务 1 -> WCF 服务 2 Con
假设永远不会直接查询数据的情况。 AKA,总会有一些必须发生的过滤逻辑和/或业务逻辑。 什么时候是在 ajax/js 之外使用数据服务的好理由? 请不要访问此页面 http://msdn.micros
我在尝试将所有常规 WCF 调用转换为异步 WCF 调用时遇到问题。我发现我重构了很多代码,但不确定具体该怎么做。我使用了我找到的方法 here但遇到了我需要事情按顺序发生的问题。 private v
我在 IIS 上有一个 WCF 服务,一些 .net Web 应用程序正在使用它。我的任务是编写一个新的 WCF 服务,要求现有的 Web 应用程序可以使用新服务而无需更改它们的 web.config
我正在尝试用外部提供 WSDL 的 WCF 等效服务替换 WSE 服务。 首先,我使用 svcutil 和 wsdl 生成所有服务和客户端类(ATP,我只关心服务实现。)我生成了一个空的 WCF 服务
场景是这样的:有2个WCF Web Services,一个是客户端(WCFClient),一个是服务端(WCFServer),部署在不同的机器上。我需要他们两个之间的证书通信。 在服务器 WCF 上,
我在 Visual Studio 2013 中创建一个 WCF 服务并将其发布到 IIS。我可以在另一个项目中添加服务引用并使用该服务的方法。当我转到 IIS 服务器管理器时,我看到 WCF 激活及其
我是 .net 的新手,对 WCF 知之甚少,如果有任何愚蠢的问题,请耐心等待。我想知道如果我的代码没有显式生成任何线程,WCF 如何处理 SELF-HOST 场景中的同时调用。因此,在阅读了很多关于
我正在为应用程序开发一个面向服务的体系结构,我希望这些服务既可以通过 WCF 公开,也可以通过一个简单的库使用。理想情况下,我想减少重复代码。 从概念上讲,这映射到: Client => WCF Se
我有一个小型测试网络服务来模拟我在现实世界应用程序中注意到的一些奇怪的东西。由于演示显示与应用程序相同的行为,为了简洁起见,我将使用演示。 简而言之,我的服务接口(interface)文件如下所示(您
我首先为我的 WCF 服务启动了我的订阅者,然后继续发布我的发布者的帖子。我的订阅者能够收到帖子。 其次,我关闭了我的第一个订阅者并再次打开它以订阅相同的服务,即所谓的已订阅该服务的第二个订阅者。再一
我是一名优秀的程序员,十分优秀!