Verification in SOAP
Visualsw
Web Service provides an open service, but in our development, you need to involve access to Web Service permissions, which requires the access to the Web Service.
With SOAP HEADER, we can simply implement privilege control:
First create a simple web service (soapCheck):
SOAPCHECK.CS (Note Red Some Notes)
Using system;
Using system.io;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.data.sqlclient;
Using system.diagnostics;
Using system.Web;
Using system.Web.services;
Using system.web.services.protocols;
Namespace Testsoap.webservice
{
[WebService (Namespace = "Testsoap.webservice",
Description = "Testsoap Web Services",
Name = "testsoap geid"]]]]
Public class soapcheck: system.web.services.Webservice
{
// Institute instantiate the Account object
Public Account Oaccount = New Account ();
Public soapcheck ()
{
InitializationComponent ();
StrConnection = getConstring ();
}
#Region Component Designer Generated Code
// Required by the Web Services Designer
Private icontainer Components = NULL;
///
/// Required Method for Designer Support - Do Not Modify
/// The contents of this method with the code editor.
/// summary>
Private vidinitiRizeComponent ()
{
}
///
/// Clean Up Any Resources Being Used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing && Components! = NULL)
{
Components.dispose ();
}
Base.dispose (Disposing);
}
#ndregion
/ / Need to add SOAP HEADER authentication to the following
[SOAPHEADER ("Oaccount")]
[WebMethod (Description = "Testsoap getReturn")]]
Public string getreturn ()
{
IF (oaccount.checkaccount ())
{
Return "Login Successed!";
}
Else
{
Return "Login Fail!";
}
}
}
}
Account class:
Inherit from SoapHeader to use SoapHeader. Account.cs
Using system;
Using system.web.services.protocols;
Namespace Testsoap.webservice
{
Public Class Account: SoapHeader
{
Public String User;
Public String Password;
Public boolean checkaccount ()
{
IF (user == "admin" && password == "admin")
{
Return True;
}
Else
{
Return False;
}
}
}
}
SOAP XML format:
XML Version = "1.0" encoding = "UTF-8"?>
Account>
soap: header>
GetGlobalempid>
soap: body>
Replace the red portion in XML with the username and password.
VB example of client call:
Public Function Testsoap (Byval Struser AS String, Byval String) AS String
DIM Objhttp as msxml2.xmlhttp
DIM STRSOAP AS STRING
DIM STRURL AS STRING
DIM XMLDoc as msxml2.domdocument
ON Error Goto Errhandle
Set objhttp = creteObject ("msxml2.xmlhttp")
Set Xmldoc = CreateObject ("msxml2.domdocument")
Strurl = "http://localhost/soapcheck.asmx"
Struser = TRIM $ (STRUSER)
StrPassword = TRIM $ (strpassword)
'Generate SOAP XML
STRSOAP = "
STRSOAP = STRSOAP & VBCRLF & " STRSOAP = STRSOAP & VBCRLF & "XMLns: SOAP =" "http://schemas.xmlsoap.org/soap/envelop/"> " STRSOAP = STRSOAP & VBCRLF & " Strsoap = strsoap & vbcrlf & "account xmlns = testsoap.webservice>" STRSOAP = STRSOAP & VBCRLF & " STRSOAP = STRSOAP & VBCRLF & " STRSOAP = STRSOAP & VBCRLF & " Account>" STRSOAP = STRSOAP & VBCRLF & " soap: header>" STRSOAP = STRSOAP & VBCRLF & " STRSOAP = strsoap & vbcrlf & " STRSOAP = STRSOAP & VBCRLF & " getreturn>" STRSOAP = STRSOAP & VBCRLF & " soap: body>" STRSOAP = STRSOAP & VBCRLF & " soap: envelope>" Objhttp.open "Post", Strurl, False Objhttp.setRequestHettp.SetRequestHeader "Content-Type", "Text / XML; Charset = UTF-8" Objhttp.setRequestHeader "Content-Length", Len (STRSOAP) Objhttp.setRequestHeader "soapaction", Strurl & "Getreturn" Objhttp.send strsoap XMLDoc.async = false XMLDoc.LoadXml (Objhttp.ResponseText) Testsoap = xmldoc.selectnodes ("// soap: envelope // SOAP: BODY / / SOAPCHECKRESPONSE // GetreturnResult"). Item (0) .textexit function Errhandle: Testsoap = "Communicate with Web Services Error" END FUNCTION