简体   繁体   English

在App.config中设置WCF ClientCredentials

[英]Set WCF ClientCredentials in App.config

Is it possible to set clientcredentials for an WCF in App.config? 是否可以在App.config中为WCF设置clientcredentials?

I would like to avoid doing this: 我想避免这样做:

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

  ...
End Using

Rather the login and password should be part of the configuration. 而是登录名和密码应该是配置的一部分。

Expanding on Ladislav Mrnka's answer, you might find this implementation useful: 扩展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;
    }
}

After which you need to register this custom implementation using something like 之后,您需要使用类似的方式注册此自定义实现

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="UserNameClientCredentials" type="MyNamespace.UserNameClientCredentials, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
  </extensions>
...

This is what I did to get the new auth to work 这就是我为了让新的auth工作所做的

Expanding further on Mormegil's answer this is how to use the customBehavior implementation. 进一步扩展Mormegil的答案,这是如何使用customBehavior实现。

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

After which you need to: 之后你需要:

  1. Register the behaviorExtension. 注册behaviorExtension。
  2. Define a new behaviorConfig using the config extension. 使用config扩展定义新的behaviorConfig。 (which was the tricky part, coverage is scarce on how to do this.) (这是一个棘手的部分,如何做到这一点的报道很少。)
  3. Apply the config to an endpoint. 将配置应用于端点。

Using something like: 使用类似的东西:

<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>

As far as I know, that is not possible using the serviceModel configuration section due to the security hole it would create. 据我所知,由于它会创建安全漏洞,因此无法使用serviceModel配置部分。 But you could create regular appSettings for these values and use them in code: 但您可以为这些值创建常规appSettings并在代码中使用它们:

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

I would advise against this approach though, unless you encrypt the configuration file. 我会建议不要使用这种方法,除非你加密配置文件。

You can try to inherit ClientCredentialsElement (handles default configuration section) and add support for UserName and Password. 您可以尝试继承ClientCredentialsElement (处理默认配置部分)并添加对UserName和Password的支持。 Than you can register this element in configuration file as behavior extension and use it instead of common configuration section. 您可以在配置文件中将此元素注册为行为扩展,并使用它而不是通用配置节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM