Welcome

首页 / 软件开发 / WCF / WCF NetTcpBinding Transport安全模式(2) 默认安全配置

WCF NetTcpBinding Transport安全模式(2) 默认安全配置2014-06-26新建一个类库名为“WcfSecurityExampleServiceLibrary”的类库项目,添加如代码清单11-10所示的契约,其中将示例契约命名为HelloService。

代码清单11-10  HelloService契约

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text; namespace WcfSecurityExampleServiceLibrary{ [ServiceContract]public interface IHelloService{[OperationContract]string GetHello();}}
代码清单11-11是HelloService契约的实现。

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text; namespace WcfSecurityExampleServiceLibrary{public class HelloService : IHelloService{public string GetHello(){ if (ServiceSecurityContext.Current != null){if (!ServiceSecurityContext.Current.IsAnonymous){ return "Hello:" + ServiceSecurityContext.Current.PrimaryIdentity.Name + ";type="+ ServiceSecurityContext.Current.PrimaryIdentity.AuthenticationType; }return "";}else{return "hello";}}}}