• wcf rest 服务用于安卓和ISO调用5-------验证


    年过完了......

    网上看到的一个验证方法,这里记录一下.直接上代码

    先编写server验证方法

     /// <summary>
            /// 校验是否有权限访问
            /// </summary>
            /// <returns></returns>
            private bool CheckIsCheckAuthorization()
            {
                //获得当前web操作上下文
                WebOperationContext woc = WebOperationContext.Current;
                //获得当前请求头中的Authorization
                if (woc == null) return true;
                var auth = woc.IncomingRequest.Headers["Authentication"];
                if (!string.IsNullOrEmpty(auth) && auth == "authentication") return true;
                woc.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed;
                return false;
            }
          public Users GetUserById(string id)
            {
                if (CheckIsCheckAuthorization())
                {
                    return _listCollection.FirstOrDefault(v => v.Id == id);
                }
                return null;
            }

    然后客户端在调用方法的时候设置Authentication的值.代码如下

     private static async void GetUserById(string id)
            {
                Console.WriteLine("-----------根据id获取用户----------");
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Add("Authentication", "authentication");
                var result = await httpClient.GetAsync("http://localhost:31572/Service1.svc/getUserById/1");
                result.EnsureSuccessStatusCode();
                Console.WriteLine(result.Content.ReadAsStringAsync().Result);
            }
    

      完成了.

  • 相关阅读:
    终端设备 tty,pty,pts 概念与文件描述符的联系
    Nginx – access_log格式及配置
    Nginx – rewrite 配置 URL重写及301跳转原理图
    nginx命令启动及选项
    nginx-web身份验证
    nginx_server_location对客户资源的辨别规则
    利用Session防止表单重复提交
    归并排序
    Cookie/Session的机制与安全
    HTTP Cookie Session
  • 原文地址:https://www.cnblogs.com/yt-007/p/5189894.html
Copyright © 2020-2023  润新知