欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C#WebService詳解

這里直接講解WebService的實(shí)現(xiàn)過程及其中應(yīng)該注意的點(diǎn),有關(guān)其應(yīng)用的環(huán)境等請(qǐng)度娘或者google。

在雙流等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)整合營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),雙流網(wǎng)站建設(shè)費(fèi)用合理。

首先新建一個(gè)WebService服務(wù):如圖:

C# WebService詳解

我的WebService的結(jié)構(gòu)如下圖:

C# WebService詳解

好了,這里主要講解身份驗(yàn)證類以及asmx服務(wù)使用身份驗(yàn)證應(yīng)該注意的問題:

身份驗(yàn)證類:(需要繼承System.Web.Services.Protocols.SoapHeader)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
namespace WebEmpty
{
    public class Authentication_WS : SoapHeader
    {
       private string userID = string.Empty;
       private string userPW = string.Empty;
       public string UserId
       {
           get { return userID; }
           set { userID = value; }
       }
       public string UserPW
       {
           get { return userPW; }
           set { userPW = value; }
       }
       public Authentication_WS()
       { }
       public Authentication_WS(string name, string password)
       {
           userID = name;
           userPW = password;
       }
       private bool IsValid(string nUserId, string nPassWord, out string nMsg)
       {
           nMsg = "";
           try
           {
               if (nUserId == "admin" && nPassWord == "admin")
               {
                   return true;
               }
               else
               {
                   nMsg = "Sorry, you have no right to call the Web service ";
                   return false;
               }
           }
           catch
           {
               nMsg = "Sorry, you have no right to call the Web service";
               return false;
           }
       }
       public bool IsValid(out string nMsg)
       {
           return IsValid(userID,userPW,out nMsg);
       }
    }
}

好了 , 加入身份驗(yàn)證也是為了讓服務(wù)更加的安全。

在服務(wù)中使用身份驗(yàn)證信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace WebEmpty
{
    /// <summary>
    /// WebAiny 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消注釋以下行。 
    //[System.Web.Script.Services.ScriptService]
    //[XmlInclude(typeof(DM_Introduce))]
    public class WebAiny : System.Web.Services.WebService
    {
        public Authentication_WS authentication = new Authentication_WS();//引入身份驗(yàn)證類
        //[OperationContract]
        //[WebGet(UriTemplate = "Add/{x}/{y}", ResponseFormat = WebMessageFormat.Xml)] 
        [WebMethod(Description="測(cè)試WebService")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        [SoapHeader("authentication")]
        public string Add(int a, int b)
        {
            string msg = "";
            if (!authentication.IsValid(out msg))
            {
                return msg;
            }
            else
            {
                return (a + b).ToString();
            }
        }
        [WebMethod(Description = "測(cè)試類型")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public void Type() {
            Context.Response.Write("OK - no SOAP xml Data"); 
        }
    }
}

(重點(diǎn))注意點(diǎn):

①Add方法使用了身份驗(yàn)證功能 , 所以此方法上需要加一個(gè)特性:[SoapHeader("authentication")]   ( authentication -》 public Authentication_WS authentication = new Authentication_WS();//引入身份驗(yàn)證類)

這個(gè)IIS web服務(wù)器配置,讀者可以搜百度自己解決。運(yùn)行程序如下:

C# WebService詳解

我建了一個(gè)控制臺(tái)程序來測(cè)試這個(gè)WebService。

是使用WebService的功能必須要引用WebService的服務(wù),引用方法步驟如下所示:


①,引入WebService

C# WebService詳解

C# WebService詳解

C# WebService詳解

C# WebService詳解

如上圖,我的WebService的引用名稱為WS

②看測(cè)試代碼 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

結(jié)果:

C# WebService詳解

需要指出的是 : Authentication_WSValue屬性是系統(tǒng)自動(dòng)生成的(就是自己的身份驗(yàn)證類后面緊加一個(gè)Value),用于設(shè)置驗(yàn)證類。

我們給一個(gè)錯(cuò)誤的賬號(hào)(密碼錯(cuò)誤):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin1";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

結(jié)果為:

C# WebService詳解

這樣就能比較好的保護(hù)自己的服務(wù)了。。。。。。

網(wǎng)站標(biāo)題:C#WebService詳解
網(wǎng)頁路徑:http://www.chinadenli.net/article28/gpccjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化關(guān)鍵詞優(yōu)化虛擬主機(jī)外貿(mào)建站動(dòng)態(tài)網(wǎng)站網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站