• 我们一起学习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        }
    服务端编码

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

    源码下载

  • 相关阅读:
    Linux 设置秘钥登录(SSH免密远程登录)
    maven profile动态选择配置文件
    PKU 1521 Entropy(简单哈弗曼树_水过)
    POJ 3253 Fence Repair(简单哈弗曼树_水过)
    XDU 1001 又是苹果(状态压缩)
    PKU 3318 Matrix Multiplication(神奇的输入)
    PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
    PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)
    PKU 1035 Spell checker(Vector+String应用)
    PKU 2002 Squares(二维点哈希+平方求余法+链地址法)
  • 原文地址:https://www.cnblogs.com/LipeiNet/p/4589176.html
Copyright © 2020-2023  润新知