使用者認證的WCF服務—範例服務程式庫(WCF Library)
在之後介紹的WCF使用者自訂帳號密碼認證所用到的WCF程式﹐都是參考到這裏所建置的WCF服務程式庫。WCF 主要透過設定來達成許多目的而不需要修改程式邏輯﹐這點WCF做的還不錯。
MyProducts.lib 專案
這個服務程式庫的專案只是為了測試WCF的呼叫﹐因此建立的方法很單純﹐一個測試資料的傳入/傳出及一個使用WCF自訂類別。
這個專案只有以下3個類別檔
Product.cs 自訂類別(DataContract 資料合約)
IProductService.cs (ServiceContract 服務合約)
ProductService.cs (實做服務)
這個專案編譯之後產生的檔案為MyProducts.lib.dll﹐後續不論使用IIS﹑WindForm 或者 Windows 服務做為載具﹐都可直接參考這個 dll 檔提供WCF服務。
Product.cs
1: namespace MyProducts.lib {
   2:      [DataContract]
	3: public class Product {
4: /// <summary>
5: /// 產品編號
6: /// </summary>
   7:          [DataMember]
	8: public string No { get; set; }
9: /// <summary>
10: /// 產品名稱
11: /// </summary>
  12:          [DataMember]
	13: public string Name { get; set; }
14: /// <summary>
15: /// 單價
16: /// </summary>
  17:          [DataMember]
	18: public int Price { get; set; }
19: /// <summary>
20: /// 數量
21: /// </summary>
  22:          [DataMember]
	23: public int Quantity { get; set; }
  24:      }
	
  25:  }
IProductService.cs
1: namespace MyProducts.lib {
   2:      [ServiceContract]
	3: public interface IProductService {
   4:          [OperationContract]
	5: string SaySomething(string value);
   6:  
	
   7:          [OperationContract]
	8: Product GetProduct(string productNo);
   9:      }
	
  10:  }
ProductService.cs
1: namespace MyProducts.lib {
2: public class ProductService : IProductService {
3: List<Product> ProductList = new List<Product>();
   4:  
	5: #region IProductService 成員
6: public string SaySomething(string value) {
7: return "You say [" + value + "].";
   8:          }
	
   9:  
	10: public Product GetProduct(string productNo) {
  11:              MakeProductData();
	
  12:  
	13: Product product = new Product();
14: try {
15: var result = from p in ProductList
16: where p.No == productNo
  17:                               select p;
	18: foreach (Product p in result) {
  19:                      product.No = p.No;
	
  20:                      product.Name = p.Name;
	
  21:                      product.Price = p.Price;
	
  22:                      product.Quantity = p.Quantity;
	
  23:                  }
	24: } catch (Exception er) {
25: throw new FaultException(er.Message);
  26:              }
	
  27:  
	28: return product;
  29:          }
	30: #endregion
  31:  
	32: /// <summary>
33: /// 產生測試資料
34: /// </summary>
35: private void MakeProductData() {
36: Product p1 = new Product();
37: p1.No = "P-001";
38: p1.Name = "Microsoft Windows communication Foundation 新一代應用程式通訊架構";
  39:              p1.Price = 680;
	
  40:              p1.Quantity = 100;
	
  41:              ProductList.Add(p1);
	
  42:  
	43: Product p2 = new Product();
44: p2.No = "P-002";
45: p2.Name = "Arduino 開發實戰指南 AVR篇";
  46:              p2.Price = 240;
	
  47:              p2.Quantity = 120;
	
  48:              ProductList.Add(p2);
	
  49:          }
	
  50:      }
	
  51:  }
UserAuthorization.lib 專案
這一個專案下只會建立一個類別﹐這個將在我們WCF自訂使用者帳號密碼認證中扮演重要的角色。
UserValidator.cs
1: namespace UserAuthorization.lib {
2: public class UserValidator : UserNamePasswordValidator {
3: public override void Validate(string userName, string password) {
4: if (userName == null || password == null) {
5: throw new ArgumentNullException();
6: } else {
7: if (!(userName == "testman" && password == "a0987")) {
8: throw new FaultException("帳號密碼錯誤。");
   9:                  }
	
  10:              }
	
  11:          }
	
  12:      }
	
  13:  }
這一個類別繼承了 UserNamePasswordValidator 用來驗證使用者的帳號和密碼﹐必須要參考System.IdentityModel 及 System.IdentityModel.Selectors。