1 SoapHeader 验证
服务端:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; namespace WebApplication2 { /// <summary> /// WebService2 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebService2 : System.Web.Services.WebService { public UserValidationSoapHeader UserValidation; [WebMethod] [SoapHeader("UserValidation")] public string HelloWorld() { return $"userName:{UserValidation.UserName}passwd:{UserValidation.Passward}"; } } public class UserValidationSoapHeader : SoapHeader { public UserValidationSoapHeader() { } public string UserName { get; set; } public string Passward { get; set; } } }
服务端配置文件开启远程访问:
<!--本地 要在web.config下的 <system.web>下添加(开启远程访问。。)--> <webServices> <protocols> <add name="HttpSoap"/> <add name="HttpPost"/> <add name="HttpGet"/> <add name="Documentation"/> </protocols> </webServices>
客户端:
static void Main(string[] args) { WebService.WebService2 service2 = new WebService.WebService2(); service2.UserValidationSoapHeaderValue = new WebService.UserValidationSoapHeader() { UserName = "hnzheng", Passward = "123" }; service2.UserValidationSoapHeaderValue.Actor = "http://www.baidu.com"; service2.HelloWorld(); Console.ReadLine(); }
2 集成身份验证
2、客户端引用代码
localhost.WebService service = new localhost.WebService();
service.Credentials = new NetworkCredential("xxx","123"); //xxx是用户名,123是密码
string result = service.HelloWorld("测试");