WebService基于SoapHeader实现安全认证(二)
支持通过Http请求方法调用webservice,同时支持SoapHeader验证。
using Globalegrow.Common; using Globalegrow.Model; using System.Web; using System.Web.Services.Protocols; namespace Globalegrow.Service { /// <summary> /// 自定义SoapHeader类。 /// </summary> public class Certificate : SoapHeader { private static ServiceConfig M_ServiceConfig; /// <summary> /// 构造函数 /// </summary> public Certificate() { //=>用于处理Http请求方式调用WebService服务所需要验证的SoapHeader信息。 this.UserName = HttpContext.Current.Request["UserName"]; this.PassWord = HttpContext.Current.Request["PassWord"]; } /// <summary> /// 静态构造函数 /// </summary> static Certificate() { //=>创建Certificate类的实例时,为AppContext.ServiceConfig 赋值 M_ServiceConfig = ServiceConfigUtility.Load(); AppContext.ServiceConfig = M_ServiceConfig; } /// <summary> /// 用户名 /// </summary> public string UserName { get; set; } /// <summary> /// 密码 /// </summary> public string PassWord { get; set; } /// <summary> /// 用户验证 /// </summary> public bool IsVaild(out string message) { string _UserName = M_ServiceConfig.WebServiceTokenAccount; string _PassWord = M_ServiceConfig.WebServiceTokenPassword; if (this.UserName == _UserName && this.PassWord == _PassWord) { message = ""; return true; } else { message = "对不起,您没有权限调用此服务!"; return false; } } } }
调用:
http://192.168.3.41:8888/PermissionService/PermissionService.asmx/GetAuthUserByRoleID?UserName=admin&PassWord=admin&roleid=11
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET