• 我们一起学习WCF 第三篇头消息验证用户身份


     前言:今天我主要写的是关于头消息的一个用处验证用户信息

    下面我画一个图,可以先看图

    第一步:我们先开始做用户请求代码

    首先:创建一个可执行的上下文对象块并定义内部传输的通道

     using (OperationContextScope scope = new OperationContextScope(userClient.InnerChannel))

    然后:创建头消息 要发送的SOAP传输的内容

    MessageHeader myHeaderUid = MessageHeader.CreateHeader(
    "header", "uid", UId);

    header:标头 XML 元素的本地名称(名字可以任意取,但是必须和服务器端保持一致)

    uid:标头 XML 元素的命名空间 URI(名字可以任意取,但是必须和服务器端保持一致)

    UId:要传送的信息(是一个object类型的值)

    最后:把创建要传输的头内容添加到传输的对象中

    OperationContext.Current.OutgoingMessageHeaders.Add(myHeaderUid);

    ok这样就完成客户端的编码

     1  ValidUserClient userClient =new ValidUserClient();
     2             while (true)
     3             {
     4                 Console.Write("用户名:");
     5                 string UId = Console.ReadLine();
     6                 Console.Write("密码:");
     7                 string Pwd = Console.ReadLine();
     8                 using (OperationContextScope scope = new OperationContextScope(userClient.InnerChannel))//创建一个可执行的上下文对象块并定义内部传输的通道
     9                 {
    10                     MessageHeader myHeaderUid = MessageHeader.CreateHeader(
    11                       "header", "uid", UId);
    12                     MessageHeader myHeaderPwd= MessageHeader.CreateHeader(
    13                            "header", "pwd", Pwd);
    14                     OperationContext.Current.OutgoingMessageHeaders.Add(myHeaderUid);
    15                     OperationContext.Current.OutgoingMessageHeaders.Add(myHeaderPwd);
    16                     Console.WriteLine(userClient.User());
    17                 }               
    18             }
    客户端编码

    第二步:服务端编码

    首先:开始检索头消息

     int uIdIndex = OperationContext.Current.IncomingMessageHeaders.FindHeader("header", "uid");(注意保持与客户端的保持一致,得到的是索引也就是头信息位置(如果index=-1就表示没有头消息))

    然后:通过索引来获取头消息内容
          Uid = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(uIdIndex);
    最后:我们就进行逻辑判断
     1   Dictionary<string,string> AllUsers =new Dictionary<string, string>(10); 
     2        public bool User()
     3        {
     4            InsertUser();//模拟数据库动态添加用户
     5            string Uid = string.Empty;
     6            string Pwd = string.Empty;
     7            bool result = false;
     8            int uIdIndex = OperationContext.Current.IncomingMessageHeaders.FindHeader("header", "uid");
     9            int pwdIndex = OperationContext.Current.IncomingMessageHeaders.FindHeader("header", "pwd");
    10            if (uIdIndex != -1)
    11            {
    12                Uid = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(uIdIndex);
    13            }
    14            if (pwdIndex!=-1)
    15            {
    16                Pwd = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(pwdIndex);
    17            }
    18            if (AllUsers.ContainsKey(Uid))
    19            {
    20                if (AllUsers[Uid] == Pwd)
    21                {
    22                    result = true;
    23                }
    24 
    25            }
    26            return result;
    27        }
    28 
    29        private void InsertUser()
    30        {
    31            AllUsers.Add("admin","123456");
    32            AllUsers.Add("sa","123456");
    33            AllUsers.Add("ldc","123456");
    34            AllUsers.Add("zd","123456");
    35        }
    服务端编码

    这样就简单的把头消息验证用户完成了,我写的有很多不好的地方大家看到了可以提出来,共同学习。下一篇着手写消息协定和数据协定。

    源码下载

  • 相关阅读:
    Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试
    Web项目中用模板Jsp页面引入所有静态样式脚本文件(js,css等)
    setInterval()、clearInterval()、setTimeout()和clearTimeout()js计数器方法
    另类的package-info.java文件探讨
    myeclipse10 如何把代码预览的窗口去掉
    phpMyAdmin中mysql的创建数据库时的编码的问题
    Jquery 中 $('obj').attr('checked',true)失效的几种解决方案
    监听<input/>标签行为的方法总结
    新花生壳+tomcat(内网映射,无需设置路由器)建站攻略
    Hibernate学习笔记(一):mycelipse建立项目流程(未完成)
  • 原文地址:https://www.cnblogs.com/LipeiNet/p/4589176.html
Copyright © 2020-2023  润新知