WCF使用用户名密码验证
服务契约
namespace WCFUserNameConstract { [ServiceContract] public interface IWcfContract { [OperationContract] bool GetOnWcfService(ref string MessageInfo); } }
服务实现
namespace WcfUserNameService { public class WcfUserNameService : IWcfContract { public bool GetOnWcfService(ref string MessageInfo) { MessageInfo = "调用服务成功了,这是返回来的!" ; return true; } } }
服务端用户名密码验证类
namespace WcfUserNameService { public class WcfValidator : System.IdentityModel.Selectors.UserNamePasswordValidator { public override void Validate(string userName, string password) { if (!(userName == "张三" && password == "123456" )) { throw new FaultException("调用服务错误,用户名或密码错误……"); } } } }
服务端配置(App.config)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <services> <service name="WcfUserNameService.WcfUserNameService" behaviorConfiguration="myBehavior"> <endpoint address="http://localhost:8181/mywcfUsername" binding="wsHttpBinding" contract="WCFUserNameConstract.IWcfContract" bindingConfiguration="myBind"></endpoint> </service> </services> <bindings> <wsHttpBinding> <binding name="myBind"> <security mode="Message"> <message clientCredentialType="UserName"/> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="myBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8181/mywcfUsername/metadata"/> <serviceCredentials> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfUserNameService.WcfValidator,WcfUserNameService"/> <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
服务端寄宿代码
namespace WcfUserNameConsole { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(WcfUserNameService.WcfUserNameService))) { host.Opened += delegate { Console.WriteLine("服务已启动,按任意键继续……"); }; host.Open(); Console.Read(); host.Close(); } } } }
客户端App.config配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <client> <endpoint address="http://localhost:8181/mywcfUsername" binding="wsHttpBinding" contract="WCFUserNameConstract.IWcfContract" name="myEnd" behaviorConfiguration="myBehavior" bindingConfiguration="mybind"></endpoint> </client> <bindings> <wsHttpBinding> <binding name="mybind"> <security mode="Message"> <message clientCredentialType="UserName"/> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="myBehavior"> <clientCredentials> <serviceCertificate> <authentication certificateValidationMode="PeerOrChainTrust"/> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>
客户端调用代码
namespace MyClient { class Program { static void Main(string[] args) { ChannelFactory<WCFUserNameConstract.IWcfContract> factory = new ChannelFactory<WCFUserNameConstract.IWcfContract>("myEnd"); UserNamePasswordClientCredential Uinfo = factory.Credentials.UserName; Uinfo.UserName = "张三"; Uinfo.Password = "123456"; string MessageInfo = "服务出错了!"; IWcfContract wcf = factory.CreateChannel(); using (wcf as IDisposable) { bool bls = wcf.GetOnWcfService(ref MessageInfo); Console.WriteLine("调用服务" + bls.ToString() + MessageInfo); Console.Read(); } } } }
注意想要实现上面的验证,须安装证书,
代码如下(如果已有证书不匹配,可运行mmc --- 文件---添加删除管理单元---证书--添加--计算机用户--下一步--确定。 然后 证书(本地计算机)--个人--证书-- 删除右边的证书,然后运行下面代码),
makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN=localhost -sky exchange -pe
certmgr.exe -add -r LocalMachine -s My -c -n localhost -r CurrentUser -s TrustedPeople
说明:上面的第一行为安装证书,第二行为符加到信任证书。