Soap身份基本認證方法

//客戶端,服務消費者。

//首先必須添加一個WebService的引用,在這裏我添加引用的命名空間是localhost。

        localhost.Service port = new localhost.Service();
        localhost.header soapHeader = new localhost.header();
        port.headerValue = soapHeader;
        soapHeader.name = tbID.Text.ToString();
        soapHeader.password = tbPassword.Text.ToString();
        Button2.Text= port.HelloWorld(); 

//服務器端,也就是服務提供者。

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class header : System.Web.Services.Protocols.SoapHeader  //SoapHeader必須繼承這個SoapHeader
{
    public string name;
    public string password;
}
public class Service : System.Web.Services.WebService
{
    public Service ()
    {

        //如果使用設計的組件,請取消註釋以下行
        //InitializeComponent();
    }

    public header head;
    [WebMethod]                     //WebService 必須申明。
    [SoapHeader("head")]    //此處必須要加SoapHeader引用
    public string HelloWorld()
    {
        if ("111" ==head.name && "222" == head.password) return "success";
        else return "failure";
    }

   
}

發佈了28 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章