gpt4 book ai didi

wcf - 在 WCF 中,对于 webHttpBinding,当服务器使用基本身份验证时,如何在客户端 web.config 中指定凭据?

转载 作者:行者123 更新时间:2023-12-02 05:23:59 25 4
gpt4 key购买 nike

我有两个 WCF RESTful 服务 - “常规”服务是公共(public)的并且没有安全性;我打算在“admin”服务中使用基于 SSL 的基本身份验证。这是我的服务器端 web.config:

<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="general" maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
<binding name="admin" maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyNamespace.AppServices.GeneralService">
<endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IGeneralService" behaviorConfiguration="web" bindingConfiguration="general" />
</service>
<service name="MyNamespace.AppServices.AdminService">
<endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IAdminService" behaviorConfiguration="web" bindingConfiguration="admin" />
</service>
</services>
</system.serviceModel>

在客户端,我目前的代码如下所示:

private static IGeneralService GetGeneralChannel()
{
WebHttpBinding binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;

WebChannelFactory<IGeneralService> cf = new WebChannelFactory<IGeneralService>(binding, new Uri("http://localhost:1066/GeneralService"));
IGeneralService channel = cf.CreateChannel();
return channel;
}

private static IAdminService GetAdminChannel()
{
WebHttpBinding binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;

WebChannelFactory<IAdminService> cf = new WebChannelFactory<IAdminService>(binding, new Uri("http://localhost:1066/AdminService"));
cf.Credentials.UserName.UserName = "myUserName";
cf.Credentials.UserName.Password = "myPassword";

IAdminService channel = cf.CreateChannel();
return channel;
}

问题是,由于我显然不想硬编码所有这些配置信息,我需要如何在客户端的 web.config 中提供它?我很清楚,绑定(bind)元素在客户端上的外观需要与在服务器上的外观几乎相同。但是,我在哪里指示分配给 WebChannelFactory 的凭据?

任何帮助和/或见解将不胜感激。

谢谢,史蒂夫

最佳答案

您无法将这些凭据(用户名和密码)放入 web.config 并让 WCF 从那里读取它们。这是 WCF 中极少数无法在配置中完成的功能之一 - 您必须在代码中设置这些凭据。

当然,在您的代码中,您可以从例如数据库表或某处的配置条目 - 但您必须自己执行此操作。 WCF 无法配置为从某处自动读取这些设置。

关于wcf - 在 WCF 中,对于 webHttpBinding,当服务器使用基本身份验证时,如何在客户端 web.config 中指定凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1928384/

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