WEB Services通过SOAP头进行安全认证,以及Silverilght调用

web服务端

1.定义一个SOAP安全类,继承SoapHeader
public class SecurityContext : SoapHeader
    {
        public string UserName
        { get; set; }

        public string UserPassWord
        { get; set; }
    }

2.在web服务中
定义一个 SecurityContext 类变量

public SecurityContext m_SecurityContext;


4.在web服务中定义一个私有方法用来验证soap头带过来的用户名密码是否正确
private bool CheckUserSecurityContext()
        {
            if (m_SecurityContext == null)
                throw new Exception("您木有用户名和密码,我不能让您通过");

            if (m_SecurityContext.UserName == "a" && m_SecurityContext.UserPassWord == "b")
                return true;
            else
                throw new Exception("您的用户名密码不对哦,您再输入试试");
        }

注意:这里我直接验证了用户名和密码为a和b,实际应用中应访问数据库或通过其他手段验证用户名密码是否正确,同时用户名和密码应进行相应的加密和解密操作。

5.在web服务对外发布方法中添加一个SoapHeader标示
[SoapHeader("m_SecurityContext")]
        [WebMethod]
        public List<Preson> GetMyPreson()
        {
            if (!CheckUserSecurityContext())
                return null;
            List<Preson> list = new List<Preson>();
            list.Add(new Preson() { UserAge = "x", UserName = "aa" });
            list.Add(new Preson() { UserName="zz", UserAge="20" });

            return list;
        }

到这里服务端处理完成。

客户端调用

1.Asp.net、Windows From等程序,在创建web services对象后
Service1 test=new Service1();
SecurityContext context=new SecurityContext(){UserName="a",UserPassWord="b"};
test.m_SecurityContext=context;
test.GetMyPreson();

2.在silverlight中取消了soap头,所以需要使用到MessageHeader,我们通过MessageHeader将soap信息传输给webservices;

步骤:
(1).定义一个SecurityContextClient类,需要继承MessageHeader
public class SecurityContextClient : MessageHeader
    {
        public string UserName
        { get; set; }

        public string UserPassWord
        { get; set; }

        protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            writer.WriteElementString("UserName",UserName);
            writer.WriteElementString("UserPassWord", UserPassWord);
        }

        public override string Name
        {
            get { return "SecurityContext"; } //为服务段SOAP头安全类名称
        }

        public override string Namespace
        {
            get { return "http://tempuri.org/"; }//web服务名称控件
        }
    }

writer.WriteElementString("UserName",UserName);
"UserName" 为web服务端SecurityContext类中的UserName属性名称
writer.WriteElementString("UserPassWord", UserPassWord);
"UserName" 为web服务端SecurityContext类中的UserPassWord属性名称

(2).在调用web服务时.
MyWebServicesTest.WebService1SoapClient client = WCFHelp.WebServices; //创建服务对象
OperationContext.Current = new OperationContext(client.InnerChannel);//得到服务对象执行通道上下文
OperationContext.Current.OutgoingMessageHeaders.Add(new SecurityContextClient()
{ UserName="a", UserPassWord="b" }); //将客户端安全类对像添加到上下文。
client.GetMyPresonAsync(); //然后就可以请求web服务了。

 

 

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值