在App.config中设置WCF ClientCredentials

时间:2010-09-16 09:20:17

标签: c# .net vb.net wcf configuration

是否可以在App.config中为WCF设置clientcredentials?

我想避免这样做:

Using svc As New MyServiceClient
  svc.ClientCredentials.UserName.UserName = "login"
  svc.ClientCredentials.UserName.Password = "pw"

  ...
End Using

相反,登录名和密码应该是配置的一部分。

4 个答案:

答案 0 :(得分:13)

扩展Ladislav Mrnka的回答,你可能会发现这个实现很有用:

public class UserNameClientCredentials : ClientCredentialsElement
{
    private ConfigurationPropertyCollection properties;

    public override Type BehaviorType
    {
        get { return typeof (ClientCredentials); }
    }

    /// <summary>
    /// Username (required)
    /// </summary>
    public string UserName
    {
        get { return (string) base["userName"]; }
        set { base["userName"] = value; }
    }

    /// <summary>
    /// Password (optional)
    /// </summary>
    public string Password
    {
        get { return (string) base["password"]; }
        set { base["password"] = value; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            if (properties == null)
            {
                ConfigurationPropertyCollection baseProps = base.Properties;
                baseProps.Add(new ConfigurationProperty(
                                  "userName",
                                  typeof (String),
                                  null,
                                  null,
                                  new StringValidator(1),
                                  ConfigurationPropertyOptions.IsRequired));
                baseProps.Add(new ConfigurationProperty(
                                  "password",
                                  typeof (String),
                                  ""));
                properties = baseProps;
            }
            return properties;
        }
    }

    protected override object CreateBehavior()
    {
        var creds = (ClientCredentials) base.CreateBehavior();
        creds.UserName.UserName = UserName;
        if (Password != null) creds.UserName.Password = Password;
        ApplyConfiguration(creds);
        return creds;
    }
}

之后,您需要使用类似

的内容注册此自定义实现
<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="UserNameClientCredentials" type="MyNamespace.UserNameClientCredentials, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
  </extensions>
...

答案 1 :(得分:9)

这就是我为使新的auth工作而做的事情

Mormegil's answer上进一步扩展这是如何使用customBehavior实现。

public class UserNameClientCredentialsElement : ClientCredentialsElement
{ // class renamed only to follow the configuration pattern
   ... // using Mormegil's implementation
}

之后你需要:

  1. 注册behaviorExtension。
  2. 使用config扩展名定义新的behaviorConfig。 (这是一个棘手的部分,如何做到这一点的覆盖面很少。)
  3. 将配置应用于端点。
  4. 使用类似的东西:

    <system.serviceModel>
      <client><!--(3)-->
        <endpoint ...YourEndpointConfig... behaviorConfiguration="UserNamePasswordBehavior" />
      </client>
      <behaviors><!--(2)-->
        <endpointBehaviors>
          <behavior name="UserNamePasswordBehavior">
            <userNameClientCredentials userName="skroob" password="12345" />
            <!--Visual Studio will give you warning squiggly on <userNameClientCredentials>
                saying that "The element 'behavior' has invalid child element" 
                but will work at runtime.-->
          </behavior>
        </endpointBehaviors>
      </behaviors>
      <extensions><!--(1)-->
        <behaviorExtensions>
          <add name="userNameClientCredentials" type="MyNamespace.UserNameClientCredentialsElement, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
      </extensions>
      ...
    </system.serviceModel>
    

答案 2 :(得分:8)

据我所知,由于它会创建安全漏洞,因此无法使用serviceModel配置部分。 但您可以为这些值创建常规appSettings并在代码中使用它们:

svc.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings("...")

我建议不要使用这种方法,除非你加密配置文件。

答案 3 :(得分:4)

您可以尝试继承ClientCredentialsElement(处理默认配置部分)并添加对UserName和Password的支持。您可以在配置文件中将此元素注册为行为扩展,并使用它而不是通用配置节。