Identity Authentication is added when WebService is called to reject unauthorized access guidance: identity authentication is added when WebService is called to deny unauthorized access as we all know, webService is an online application service for enterprises. Other companies or applications can access and use this online service over the Internet. But sometimes ......
Add authentication when WebService is called to reject unauthorized access
As we all know, WebService is an online application service for enterprises. Other companies or applications can access and use this online service over the Internet. However, in some cases, some application services do not want to be accessed without authorization. In this case, we can perform authentication in several ways.
Method 1: Introduce SoapHeader in WebService
View plaincopy to clipboardprint?
# Configure the logon header for region
/// <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 username | Password
/// <Summary>
/// User Name
/// </Summary>
Public string UserName
{
Get {return strUserName ;}
Set {strUserName = value ;}
}
/// <Summary>
/// Password
/// </Summary>
Public string PassWord
{
Get {return strPassWord ;}
Set {strPassWord = value ;}
}
# Endregion
# Region check for correct Logon
/// <Summary>
/// Check whether the logon is correct
/// </Summary>
/// <Returns> </returns>
Public bool CheckLogin ()
{
If (strUserName = "valid logon name" & strPassWord = "valid logon password ")
{
Return true;
}
Else
{
Return false;
}
}
# Endregion
}
# Endregion
# Configure the logon header for region
/// <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 username | Password
/// <Summary>
/// User Name
/// </Summary>
Public string UserName
{
Get {return strUserName ;}
Set {strUserName = value ;}
}
/// <Summary>
/// Password
/// </Summary>
Public string PassWord
{
Get {return strPassWord ;}
Set {strPassWord = value ;}
}
# Endregion
# Region check for correct Logon
/// <Summary>
/// Check whether the logon is correct
/// </Summary>
/// <Returns> </returns>
Public bool CheckLogin ()
{
If (strUserName = "valid logon name" & strPassWord = "valid logon password ")
{
Return true;
}
Else
{
Return false;
}
}
# Endregion
}
# Endregion
Add a service for testing:
View plaincopy to clipboardprint?
# Region test connection
[System. Web. Services. Protocols. SoapHeader ("myHeader")]
[WebMethod (Description = "determine whether a user has activated", EnableSession = true)]
Public string _ GetValue (string strInputValue)
{
If (myHeader. CheckLogin ())
{
String strReturnValue = strInputValue + "@ CopyRight By bankao 2010 ";
Return strReturnValue;
}
Else
{
Return "invalid authentication. Please try again! ";
}
}
# Endregion
# Region test connection
[System. Web. Services. Protocols. SoapHeader ("myHeader")]
[WebMethod (Description = "determine whether a user has activated", EnableSession = true)]
Public string _ GetValue (string strInputValue)
{
If (myHeader. CheckLogin ())
{
String strReturnValue = strInputValue + "@ CopyRight By bankao 2010 ";
Return strReturnValue;
}
Else
{
Return "invalid authentication. Please try again! ";
}
}
# Endregion
Now we have configured the service to pass authentication. Let's perform some tests to create a new webForm in Page_Load:
View plaincopy to clipboardprint?
WebLogon. MySoapHeader myHeader = new WebLogon. MySoapHeader ();
MyHeader. UserName = "the specified legal user ";
MyHeader. PassWord = "the specified valid 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 = "the specified legal user ";
MyHeader. PassWord = "the specified valid 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 ."));
When running this WebForm, if the user name and password are correct, we will see:
This is BanLao's Test Application For soapheaders. @ CopyRight By BanLao 2010
Otherwise
Invalid authentication. Please try again!
Method 2: verify Web Service by Session
View plaincopy to clipboardprint?
[WebMethod (Description = "check whether logon is correct", EnableSession = true)]
Public bool CheckLogin (string strUserName, string strPassword)
{
If (strUserName. Equals ("admin") & amp; strPassword. Equals ("123456 "))
{
Session ["LoginState"] = true;
}
Else
{
Session ["LoginState"] = false;
}
Return (bool) Session ["LoginState"];
}
# Region test connection
[WebMethod (Description = "test connection", EnableSession = true)]
Public string _ GetValue (string strInputValue)
{
If (Session ["LoginState"] = null | Session ["LoginState"]. Equals (false ))
{
Return "invalid authentication. Please try again! ";
}
Else
{
String strReturnValue = strInputValue + "@ CopyRight By bankao 2010 ";
Return strReturnValue;
}
}
# Endregion
[WebMethod (Description = "check whether logon is correct", EnableSession = true)]
Public bool CheckLogin (string strUserName, string strPassword)
{
If (strUserName. Equals ("admin") & amp; strPassword. Equals ("123456 "))
{
Session ["LoginState"] = true;
}
Else
{
Session ["LoginState"] = false;
}
Return (bool) Session ["LoginState"];
}
# Region test connection
[WebMethod (Description = "test connection", EnableSession = true)]
Public string _ GetValue (string strInputValue)
{
If (Session ["LoginState"] = null | Session ["LoginState"]. Equals (false ))
{
Return "invalid authentication. Please try again! ";
}
Else
{
String strReturnValue = strInputValue + "@ CopyRight By bankao 2010 ";
Return strReturnValue;
}
}
# Endregion
Call this service,
View plaincopy to clipboardprint?
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 ."));
}
When running this WebForm, if the user name and password are correct, we will see:
This is BanLao's Test Application For Session. @ CopyRight By BanLao 2010
Otherwise
Invalid authentication. Please try again!
NOTE: If multiple legal users are required, you can declare and determine in WebService.