首页 > 移动平台 > 详细

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

时间:2016-02-15 09:21:29      阅读:258      评论:0      收藏:0      [点我收藏+]

年过完了......

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

先编写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);
        }

 

  完成了.

 

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

原文:http://www.cnblogs.com/yt-007/p/5189894.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!