标签:
年过完了......
网上看到的一个验证方法,这里记录一下.直接上代码
先编写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