WCF基于用户名和密码安全(升级)纯代码

 为了便使这种方式自由度更高,今天我就把这个服务器端纯代码化了。

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using System.IdentityModel.Selectors;
  7. using System.ServiceModel.Description;
  8. namespace wcf.username
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             EndpointAddress endp = new EndpointAddress("http://localhost/myservice");
  15.             WSHttpBinding ws = new WSHttpBinding();
  16.             ws.Security.Mode = SecurityMode.Message;
  17.             ws.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
  18.             ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
  19.             behavior.HttpGetEnabled = true;
  20.             behavior.HttpGetUrl = new Uri("http://localhost/myservice/mex");
  21.             ServiceHost sh = new ServiceHost(typeof(serverdo), new Uri(endp.ToString()));
  22.             sh.Description.Behaviors.Add(behavior);
  23.             sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode = 
  24.                 System.ServiceModel.Security.X509CertificateValidationMode.None;
  25.             sh.Credentials.UserNameAuthentication.UserNamePasswordValidationMode =
  26.                  System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
  27.             sh.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new checkUserName();
  28.             sh.Credentials.ServiceCertificate.SetCertificate(
  29.                 System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, 
  30.                 System.Security.Cryptography.X509Certificates.StoreName.My,
  31.                 System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "MyServer");
  32.             sh.AddServiceEndpoint(typeof(Iservice), ws, endp.ToString());
  33.             sh.Open();
  34.             Console.WriteLine("ok");
  35.             Console.Read();
  36.         }
  37.     }
  38.     class checkUserName:UserNamePasswordValidator
  39.     {
  40.         public override void Validate(string userName, string password)
  41.         {
  42.             if (userName != "jac" && password != "jac")
  43.             {
  44.                 throw new FaultException("userName and passWord be error");
  45.             }
  46.         }
  47.     }
  48.     [ServiceContract]
  49.     interface Iservice
  50.     {
  51.         [OperationContract]
  52.         string test(string msg);
  53.     }
  54.     class serverdo:Iservice
  55.     {
  56.         #region Iservice 成员
  57.         public string test(string msg)
  58.         {
  59.             Console.WriteLine("user inter:" + msg);
  60.             return "user inter:" + msg;
  61.         }
  62.         #endregion
  63.     }
  64. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值