Mở rộng về câu trả lời Ladislav Mrnka, bạn có thể tìm thấy thi này hữu ích:
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;
}
}
Sau đó bạn cần phải đăng ký thực hiện phong tục này sử dụng một cái gì đó giống như
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="UserNameClientCredentials" type="MyNamespace.UserNameClientCredentials, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
...
Cảm ơn. Nhưng việc lưu trữ thông tin đăng nhập trong AppSettings sẽ vẫn yêu cầu tôi đặt các giá trị theo lập trình. Tôi chắc chắn nó có thể là một vấn đề an ninh, nhưng tôi chỉ không thấy sự khác biệt: Mọi người sẽ lưu trữ đăng nhập/pw một nơi nào đó anyways - tại sao không phải ở đó cùng với phần còn lại của cấu hình WCF ?? :) –
Như bạn đã nói, nó liên quan đến an ninh. Cung cấp cho người dùng một cách để chỉ định một mật khẩu trong văn bản rõ ràng là một lỗ hổng bảo mật rõ ràng. Bây giờ, nếu nhà phát triển quyết định bỏ qua nó bằng mã tôi đã cung cấp, anh ta sẽ nhận thức được việc mình làm sai. Anh ấy sẽ không thể nói "Hey Microsoft, lỗi của bạn, bạn nói nó là OK để đặt nó trong cấu hình WCF." –