gpt4 book ai didi

wcf - 如何将服务引用与基本身份验证 WCF SOAP 服务一起使用

转载 作者:行者123 更新时间:2023-12-04 12:11:43 25 4
gpt4 key购买 nike

我有一个使用基本访问身份验证的 WCF SOAP 服务。未使用 SSL - 我了解这里的安全问题。

使用 WCFTestClient 应用程序,我通过临时硬编码到服务中的用户名和密码来验证服务是否正常工作,以便在 Authorization 时使用。 header 不存在。

我现在正在尝试编写一个通过 Authorization 传递凭据的测试应用程序。标题。我在我的测试应用程序中添加了对我的服务的服务引用,但是 Authorization http请求中不存在 header 。生成的MyServiceClient类使用 System.ServiceModel.ClientBase
在我的测试应用程序中,我将凭据设置如下

MyServiceClient client = new MyServiceClient("BasicHttpBinding_MyService");
client.ClientCredentials.UserName.UserName = "WebServiceUsername";
client.ClientCredentials.UserName.Password = "WebServicepassword";

我也试过如下
MyServiceClient client = new MyServiceClient();
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "WebServiceUsername";
loginCredentials.UserName.Password = "WebServicepassword";
client.Endpoint.Behaviors.Remove(client.Endpoint.Behaviors.Find<ClientCredentials>());
client.Endpoint.Behaviors.Add(loginCredentials);

服务web.config如下
<services>
<service name="MyService" behaviorConfiguration="MyBehavior" >
<endpoint contract="MyService" binding="basicHttpBinding" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>

测试app.config如下
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MyService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55314/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyService"
contract="MyService" name="BasicHttpBinding_MyService" />
</client>
</system.serviceModel>
</configuration>

对我所缺少的有什么想法吗?

最佳答案

这是一个很好的起点,将您的绑定(bind)和端点信息从配置文件移动到您的类:

        protected BasicHttpBinding binding = new BasicHttpBinding()
{
Name = "Name your binding here",
CloseTimeout = new TimeSpan(0, 1, 0),
OpenTimeout = new TimeSpan(0, 1, 0),
ReceiveTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 1, 0),
AllowCookies = false,
BypassProxyOnLocal = false,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferSize = 65536,
MaxBufferPoolSize = 524288,
MaxReceivedMessageSize = 65536,
MessageEncoding = WSMessageEncoding.Text,
TransferMode = TransferMode.Buffered,
UseDefaultWebProxy = true,
Security = new BasicHttpSecurity()
{
Mode = BasicHttpSecurityMode.Transport,
Message = new BasicHttpMessageSecurity() { AlgorithmSuite = SecurityAlgorithmSuite.Default, ClientCredentialType = BasicHttpMessageCredentialType.UserName},
Transport = new HttpTransportSecurity() { ClientCredentialType = HttpClientCredentialType.Digest }
},
};
protected EndpointAddress endPoint = new EndpointAddress("http://localhost:55314/MyService.svc");

进而
MyServiceClient client = new MyServiceClient(binding, endpont);

试试这个,并根据您的需要调整绑定(bind),尤其是“安全性”。

关于wcf - 如何将服务引用与基本身份验证 WCF SOAP 服务一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827754/

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