代码之家  ›  专栏  ›  技术社区  ›  Jakob Gade

在中设置WCF客户端凭据应用程序配置

  •  10
  • Jakob Gade  · 技术社区  · 14 年前

    我想避免这样做:

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

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

    4 回复  |  直到 14 年前
        1
  •  8
  •   Johann Blais    14 年前

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

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

        2
  •  15
  •   Mormegil    13 年前

    展开Ladislav Mrnkas answer,您可能会发现此实现非常有用:

    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>
    ...
    
        3
  •  10
  •   RubberChickenLeader    7 年前

    这就是我所做的,让新的授权工作

    进一步扩大 Mormegil's answer

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

    之后您需要:

    1. 注册behaviorExtension。
    2. (这是一个棘手的部分,关于如何做到这一点的报道很少。)
    3. 将配置应用于终结点。

    使用类似于:

    <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>
    
        4
  •  5
  •   Ladislav Mrnka    14 年前

    你可以试着继承 ClientCredentialsElement