生活随笔
收集整理的這篇文章主要介紹了
调用WebService时加入身份验证,以拒绝未授权的访问
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
調(diào)用WebService時(shí)加入身份驗(yàn)證,以拒絕未授權(quán)的訪問(wèn) 分類: WebService 2010-08-19 16:22 548人閱讀 收藏 舉報(bào) ????? 眾所周知,WebService是為企業(yè)需求提供的在線應(yīng)用服務(wù),其他公司或應(yīng)用軟件能夠通過(guò)Internet來(lái)訪問(wèn)并使用這項(xiàng)在線服務(wù)。但在有些時(shí)候的某些應(yīng)用服務(wù)不希望被未授權(quán)訪問(wèn),那么此時(shí)我們可以一下幾種方法來(lái)實(shí)現(xiàn)身份驗(yàn)證。
?
方法一:在WebService中引入SoapHeader
[c-sharp] view plain copy print ? #region?配置登錄標(biāo)頭 ???? ?? ?? public ?class ?MySoapHeader?:?SoapHeader??{?? ????private ?string ?strUserName?=?string .Empty;?? ????private ?string ?strPassWord?=?string .Empty;?? ?? ????public ?MySoapHeader()?{?}?? ?? ????public ?MySoapHeader(string ?username,?string ?password)?? ????{?? ????????this .strUserName?=?username;?? ????????this .strPassWord?=?password;?? ????} ? ?????#region?構(gòu)造?用戶名|密碼 ???????? ?????? ?????? ????public ?string ?UserName?? ????{?? ????????get ?{?return ?strUserName;?}?? ????????set ?{?strUserName?=?value;?}?? ????}?? ?????? ?????? ?????? ????public ?string ?PassWord?? ????{?? ????????get ?{?return ?strPassWord;?}?? ????????set ?{?strPassWord?=?value;?}?? ????} ? ?????#endregion ? ?????#region?檢測(cè)是否正確登錄 ???????? ?????? ?????? ?????? ????public ?bool ?CheckLogin()?? ????{?? ????????if ?(strUserName?==?"合法登錄名" ?&&?strPassWord?==?"合法登錄密碼" )?? ????????{?? ????????????return ?true ;?? ????????}?? ????????else ?? ????????{?? ????????????return ?false ;?? ????????}?? ????} ? ?????#endregion ??} ? #endregion ??#region 配置登錄標(biāo)頭 /// <summary> /// Code CreateBy BanLao /// </summary> public class MySoapHeader : SoapHeader { private string strUserName = string.Empty; private string strPassWord = string.Empty; public MySoapHeader() { } public MySoapHeader(string username, string password) { this.strUserName = username; this.strPassWord = password; } #region 構(gòu)造 用戶名|密碼 /// <summary> /// 用戶名 /// </summary> public string UserName { get { return strUserName; } set { strUserName = value; } } /// <summary> /// 密碼 /// </summary> public string PassWord { get { return strPassWord; } set { strPassWord = value; } } #endregion #region 檢測(cè)是否正確登錄 /// <summary> /// 檢測(cè)是否正確登錄 /// </summary> /// <returns></returns> public bool CheckLogin() { if (strUserName == "合法登錄名" && strPassWord == "合法登錄密碼") { return true; } else { return false; } } #endregion } #endregion
?
?
加入一個(gè)服務(wù)用于測(cè)試:
?
[c-sharp] view plain copy print ? #region?測(cè)試連接 ???[System.Web.Services.Protocols.SoapHeader("myHeader" )]?? ?[WebMethod(Description?=?"判斷用戶是否開(kāi)通" ,?EnableSession?=?true )]?? ?public ?string ?_GetValue(string ?strInputValue)?? ?{?? ?????if ?(myHeader.CheckLogin())?? ?????{?? ?????????string ?strReturnValue?=?strInputValue?+?"@CopyRight?By?BanLao?2010" ;?? ?????????return ?strReturnValue;?? ?????}?? ?????else ?? ?????{?? ?????????return ?"無(wú)效的身份驗(yàn)證,請(qǐng)重試!" ;?? ?????}?? ?} ? ?#endregion ?? #region 測(cè)試連接 [System.Web.Services.Protocols.SoapHeader("myHeader")] [WebMethod(Description = "判斷用戶是否開(kāi)通", EnableSession = true)] public string _GetValue(string strInputValue) { if (myHeader.CheckLogin()) { string strReturnValue = strInputValue + "@CopyRight By BanLao 2010"; return strReturnValue; } else { return "無(wú)效的身份驗(yàn)證,請(qǐng)重試!"; } } #endregion
?
?
至此我們想要的需要通過(guò)身份驗(yàn)證的服務(wù)配置好了,下面讓我們進(jìn)行一些測(cè)試,新建一個(gè)webForm在Page_Load中:
??
[c-sharp] view plain copy print ? WebLogon.MySoapHeader?myHeader?=?new ?WebLogon.MySoapHeader();?? myHeader.UserName?=?"約定的合法用戶" ;?? myHeader.PassWord?=?"約定的合法密碼" ;?? ?? WebLogon.Service?This_Service?=?new ?WebLogon.Service();?? This_Service.MySoapHeaderValue?=?myHeader;?? Response.Write(This_Service._GetValue("This?is?BanLao's?Test?Application?For?SoapHeader.?" ));?? WebLogon.MySoapHeader myHeader = new WebLogon.MySoapHeader(); myHeader.UserName = "約定的合法用戶"; myHeader.PassWord = "約定的合法密碼"; WebLogon.Service This_Service = new WebLogon.Service(); This_Service.MySoapHeaderValue = myHeader; Response.Write(This_Service._GetValue("This is BanLao's Test Application For SoapHeader. "));
?
?
當(dāng)運(yùn)行這個(gè)WebForm時(shí),如果用戶名和密碼是正確的我們將看到:
?
This is BanLao's Test Application For SoapHeader.?@CopyRight By BanLao 2010
?
否則
?
無(wú)效的身份驗(yàn)證,請(qǐng)重試!
?
?
方法二:Web Service以Session方式驗(yàn)證
?
[c-sharp] view plain copy print ? [WebMethod(Description?=?"檢測(cè)是否正確登錄" ,?EnableSession?=?true )]?? public ?bool ?CheckLogin(string ?strUserName,?string ?strPassword)??{?? ????if ?(strUserName.Equals("admin" )?&&?strPassword.Equals("123456" ))?? ????{?? ????????Session["LoginState" ]?=?true ;?? ????}?? ????else ?? ????{?? ????????Session["LoginState" ]?=?false ;?? ????}?? ????return ?(bool )Session["LoginState" ];?? } ? ?#region?測(cè)試連接 ??[WebMethod(Description?=?"測(cè)試連接" ,?EnableSession?=?true )]?? public ?string ?_GetValue(string ?strInputValue)??{?? ????if ?(Session["LoginState" ]?==?null ?||?Session["LoginState" ].Equals(false ))?? ????{?? ????????return ?"無(wú)效的身份驗(yàn)證,請(qǐng)重試!" ;?? ????}?? ????else ?? ????{?? ????????string ?strReturnValue?=?strInputValue?+?"@CopyRight?By?BanLao?2010" ;?? ????????return ?strReturnValue;?? ????}?? } ? #endregion ?? [WebMethod(Description = "檢測(cè)是否正確登錄", EnableSession = true)] public bool CheckLogin(string strUserName, string strPassword) { if (strUserName.Equals("admin") && strPassword.Equals("123456")) { Session["LoginState"] = true; } else { Session["LoginState"] = false; } return (bool)Session["LoginState"]; } #region 測(cè)試連接 [WebMethod(Description = "測(cè)試連接", EnableSession = true)] public string _GetValue(string strInputValue) { if (Session["LoginState"] == null || Session["LoginState"].Equals(false)) { return "無(wú)效的身份驗(yàn)證,請(qǐng)重試!"; } else { string strReturnValue = strInputValue + "@CopyRight By BanLao 2010"; return strReturnValue; } } #endregion
?
?
調(diào)用該服務(wù),
?
[c-sharp] view plain copy print ? WebLogon.Service?This_Service?=?new ?WebLogon.Service();?? This_Service.CookieContainer?=?new ?System.Net.CookieContainer();?? if ?(This_Service.CheckLogin("admin" ,?"123456" ))??{?? ????Response.Write(This_Service._GetValue("This?is?BanLao's?Test?Application?For?Session.?" ));?? }?? WebLogon.Service This_Service = new WebLogon.Service(); This_Service.CookieContainer = new System.Net.CookieContainer(); if (This_Service.CheckLogin("admin", "123456")) { Response.Write(This_Service._GetValue("This is BanLao's Test Application For Session. ")); }
?
?
當(dāng)運(yùn)行這個(gè)WebForm時(shí),如果用戶名和密碼是正確的我們將看到:
?
This is BanLao's Test Application For Session.?@CopyRight By BanLao 2010
?
否則
?
無(wú)效的身份驗(yàn)證,請(qǐng)重試!
?
?
注:如果需要多個(gè)合法用戶,可以在WebService中聲明判斷即可~
轉(zhuǎn)載于:https://www.cnblogs.com/fx2008/archive/2012/06/02/2532139.html
總結(jié)
以上是生活随笔 為你收集整理的调用WebService时加入身份验证,以拒绝未授权的访问 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。