新聞中心
WCF是.NET Framework 3.5的重要組成部分,主要作用域通信方面。我們可以通過使用它來輕松的完成一些特定功能需求。在這里我們將會針對WCF安全參數(shù)的相關設置方法,為大家詳解有關內容。

創(chuàng)新互聯(lián)建站,為您提供網(wǎng)站建設、成都網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設計,對服務地磅秤等多個行業(yè)擁有豐富的網(wǎng)站建設及推廣經(jīng)驗。創(chuàng)新互聯(lián)建站網(wǎng)站建設公司成立于2013年,提供專業(yè)網(wǎng)站制作報價服務,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進步,是我們永遠的責任!
WCF安全參數(shù)設置方法1. 安全方式
通過設置 Binding 的屬性 Security 來實現(xiàn)。
- NetTcpBinding binding = new NetTcpBinding();
- binding.Security.Mode = SecurityMode.Transport;
- binding.Security.Transport.ProtectionLevel = System.Net.
Security.ProtectionLevel.EncryptAndSign;
WCF安全參數(shù)設置方法2. 消息保護
通過 ServiceContractAttribute 和 OperationContractAttribute 特性的 ProtectionLevel 參數(shù)我們可以設置不同的消息保護級別。
- [ServiceContract(ProtectionLevelProtectionLevel =
ProtectionLevel.EncryptAndSign)]- interface IMyContract
- {
- ...
- }
WCF安全參數(shù)設置方法3. 身份驗證
不同的部署環(huán)境,會采取不同的選擇。在 Intranet 環(huán)境下,我們可能選擇 Windows 集成驗證方式,而在 Internet 環(huán)境下通常的方案是采取 X.509 數(shù)字證書,當然最最通用最最常見依然是用戶名/密碼。
以 Windows 集成驗證為例,客戶端可以通過 ClientBase.ClientCredentials 屬性向服務器端發(fā)送與其相匹配的身份驗證信息。缺省情況下,客戶端使用當前 Windows 登錄賬戶作為身份驗證信息,我們也可以顯式設置不同的身份信息。
代理方式:
- NetworkCredential credentials = new NetworkCredential( );
- credentials.Domain = "MyDomain";
- credentials.UserName = "MyUsername";
- credentials.Password = "MyPassword";
- using (MyContractClient client = new MyContractClient())
- {
- client.ClientCredentials.Windows.ClientCredential = credentials;
- client.MyMethod( );
- }
工廠方式:
- ChannelFactory
factory = new ChannelFactory (""); - factory.Credentials.Windows.ClientCredential = new NetworkCredential(...);
- IMyContract client = factory.CreateChannel( );
- using(client as IDisposable)
- {
- client.MyMethod( );
- }
在服務中,我們可以用 ServiceSecurityContext.Current (或者 OperationContext.Current.ServiceSecurityContext) 來獲取相關身份信息。
- Console.WriteLine(ServiceSecurityContext.Current.
WindowsIdentity.AuthenticationType);- Console.WriteLine(ServiceSecurityContext.Current.
WindowsIdentity.Name);
WCF安全參數(shù)的相關設置方法就為大家介紹到這里。
網(wǎng)站欄目:WCF安全參數(shù)相關設置方法詳解
當前地址:http://fisionsoft.com.cn/article/copihgo.html


咨詢
建站咨詢
