[英]Set WCF ClientCredentials in App.config
是否可以在App.config中為WCF設置clientcredentials?
我想避免這樣做:
Using svc As New MyServiceClient
svc.ClientCredentials.UserName.UserName = "login"
svc.ClientCredentials.UserName.Password = "pw"
...
End Using
而是登錄名和密碼應該是配置的一部分。
擴展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>
...
這就是我為了讓新的auth工作所做的
進一步擴展Mormegil的答案,這是如何使用customBehavior實現。
public class UserNameClientCredentialsElement : ClientCredentialsElement
{ // class renamed only to follow the configuration pattern
... // using Mormegil's implementation
}
之后你需要:
使用類似的東西:
<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>
據我所知,由於它會創建安全漏洞,因此無法使用serviceModel配置部分。 但您可以為這些值創建常規appSettings並在代碼中使用它們:
svc.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings("...")
我會建議不要使用這種方法,除非你加密配置文件。
您可以嘗試繼承ClientCredentialsElement (處理默認配置部分)並添加對UserName和Password的支持。 您可以在配置文件中將此元素注冊為行為擴展,並使用它而不是通用配置節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.