2010-09-16 40 views
9

Có thể đặt clientcredentials cho WCF trong App.config không?Đặt WCF ClientCredentials trong App.config

Tôi muốn tránh làm điều này:

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

    ... 
End Using 

Thay tên đăng nhập và mật khẩu nên là một phần của cấu hình.

Trả lời

8

Theo như tôi biết, điều đó là không thể bằng cách sử dụng phần cấu hình dịch vụModel do lỗ hổng bảo mật mà nó sẽ tạo ra. Nhưng bạn có thể tạo appSettings thường xuyên cho các giá trị và sử dụng chúng trong mã:

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

tôi sẽ tư vấn cho chống lại phương pháp này tuy nhiên, trừ khi bạn mã hóa các tập tin cấu hình.

+1

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 ?? :) –

+1

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." –

4

Bạn có thể cố gắng kế thừa ClientCredentialsElement (xử lý phần cấu hình mặc định) và thêm hỗ trợ cho UserName và Mật khẩu. Hơn bạn có thể đăng ký phần tử này trong tệp cấu hình dưới dạng phần mở rộng hành vi và sử dụng nó thay vì phần cấu hình chung.

12

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> 
... 
+0

Cảm ơn, nó trông giống như một thực hiện khá gọn gàng. Tôi sẽ thử nó một ngày nào đó. :) –

8

Đây là những gì tôi đã làm để có được auth mới hoạt động

Mở rộng thêm trên Mormegil's answer đây là cách sử dụng triển khai customBehavior.

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

Sau đó bạn cần phải:

  1. Đăng ký behaviorExtension.
  2. Xác định hành vi mớiConfig bằng tiện ích cấu hình. (đó là một phần khó khăn, phạm vi bảo hiểm là khan hiếm về cách làm điều này.)
  3. Áp dụng cấu hình cho điểm cuối.

Sử dụng một cái gì đó như:

<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> 
Các vấn đề liên quan