Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WCF中,对于webHttpBinding,当服务器使用基本身份验证时,如何在客户端web.config中指定凭据?_Wcf_Web Config_Credentials_Webhttpbinding_Webchannelfactory - Fatal编程技术网

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

在WCF中,对于webHttpBinding,当服务器使用基本身份验证时,如何在客户端web.config中指定凭据?,wcf,web-config,credentials,webhttpbinding,webchannelfactory,Wcf,Web Config,Credentials,Webhttpbinding,Webchannelfactory,我有两个WCF RESTful服务——“通用”服务是公共的,没有安全性;“管理员”服务我打算通过SSL使用基本身份验证。这是我的服务器端web.config: <system.serviceModel> <bindings> <webHttpBinding> <binding name="general" maxReceivedMessageSize="2147483647">

我有两个WCF RESTful服务——“通用”服务是公共的,没有安全性;“管理员”服务我打算通过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;
}
private静态IGeneralService GetGeneralChannel()
{
WebHttpBinding=新的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 cf=新的WebChannelFactory(绑定,新Uri(“http://localhost:1066/GeneralService"));
IGeneralService通道=cf.CreateChannel();
返回通道;
}
私有静态IAdminService GetAdminChannel()
{
WebHttpBinding=新的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 cf=新的WebChannelFactory(绑定,新Uri(“http://localhost:1066/AdminService"));
cf.Credentials.UserName.UserName=“myUserName”;
cf.Credentials.UserName.Password=“myPassword”;
IAdminService channel=cf.CreateChannel();
返回通道;
}
问题是,由于我显然不想硬编码所有这些配置信息,我需要如何在客户端的web.config中提供这些信息?我很清楚,绑定元素在客户机上的外观必须与在服务器上的外观基本相同。但是,我在哪里指示分配给WebChannel Factory的凭据

如有任何帮助和/或见解,将不胜感激

谢谢,
Steve

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


当然,在您的代码中,您可以从数据库表或某个配置条目中读取它们,但您必须自己读取。WCF无法配置为从某处自动读取这些设置。

这就解释了为什么在到处搜索之后,我在WCF配置模式中找不到任何类似用户名/密码元素的内容-他感谢提供的信息。将这些值放在appSettings元素中也很容易。