Go WebService head encryption, you canWebService set Access user name and password, enhance the security of WebService so that webservice can only be used by authorized users.
Specific implementation steps:
1. define a SoapHeader derived class to implement WebService access validation
[CSharp]View Plaincopy
- <span style="font-size:10px;" >using System;
- Using System.Data;
- Using System.Configuration;
- Using System.Web;
- Using System.Web.Security;
- Using System.Web.UI;
- Using System.Web.UI.HtmlControls;
- Using System.Web.UI.WebControls;
- Using System.Web.UI.WebControls.WebParts;
- <summary>
- Summary description of Mysoapheader
- </summary>
- Public class Mysoapheader:system. Web. Services. Protocols. SoapHeader
- {
- private String _uname = string. Empty; //webservice Access User name
- public string Uname
- {
- get { return _uname;}
- set {_uname = value;}
- }
- private String _password = string. Empty; //webservice access password
- public string Password
- {
- get { return _password;}
- set {_password = value;}
- }
- Public Mysoapheader ()
- {
- //
- //todo: Add constructor logic here
- //
- }
- Public Mysoapheader (string uname, string upass)
- {
- Init (uname, UPass);
- }
- private void init (string uname, string upass)
- {
- This._password = UPass;
- this._uname = uname;
- }
- //Verify that users have access to the internal interface
- Private bool IsValid (string uname, string upass, out string msg)
- {
- msg = "";
- if (uname = = "Admin" && upass = ="admin")
- {
- return true;
- }
- else {
- msg = "Sorry! You do not have permission to call this webservice! ";
- return false;
- }
- }
- //Verify that users have access to external interfaces
- Public bool IsValid (out string msg)
- {
- return IsValid (_uname, _password, outmsg);
- }
- }
- </span>
2, define the need to verify the WebService.
[CSharp]View Plaincopy
- <span style="font-size:10px;" >using System;
- Using System.Collections;
- Using System.Linq;
- Using System.Web;
- Using System.Web.Services;
- Using System.Web.Services.Protocols;
- Using System.Xml.Linq;
- <summary>
- Summary description of Test
- </summary>
- [WebService (Namespace = "http://tempuri.org/")]
- [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
- To allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
- [System.Web.Script.Services.ScriptService]
- Public class Test:System.Web.Services.WebService {
- Public Test () {
- //If you are using a design component, uncomment the following line
- //initializecomponent ();
- }
- Public Mysoapheader myheader = new Mysoapheader ();
- [WebMethod]
- public String HelloWorld () {//Normal WebService, no verification required
- return "Hello World";
- }
- [SoapHeader ("MyHeader")]//Join this header WebService need to verify, not add to the normal WebService no need to verify
- [WebMethod (Description = "The price of the product is queried according to the product number", EnableSession = true)]
- public string GetProductPrice2 (string ProductId)
- {
- string msg = "";
- //Verify that you have access to
- if (!myheader.isvalid (outmsg))
- {
- return-1; //Return error message
- }
- return ProductId;
- }
- }
- </span>
3. Client Call Method
The reference WebService defines the WebService name as : MyService
[CSharp]View Plaincopy
- <span style="font-size:10px;" >using System;
- Using System.Configuration;
- Using System.Data;
- Using System.Web;
- Using System.Web.Security;
- Using System.Web.UI;
- Using System.Web.UI.HtmlControls;
- Using System.Web.UI.WebControls;
- Using System.Web.UI.WebControls.WebParts;
- Public partial class _default:system.web.ui.page
- {
- protected void Page_Load (object sender, EventArgs e)
- {
- Myservice.test te = new Abc.test ();
- MyService. Mysoapheader myhead = new Mysoapheader ();
- Myhead. Uname = "admin"; Enter WebService Access user name
- Myhead. Password = "admin"; Enter WebService access password
- Te. Mysoapheadervalue = Myhead; //
- string test = Te. GetProductPrice2 ("ok!");
- Response.Write (AA); //username, password input correct output OK otherwise output error msg
- }