Recently, when I was working on a program's Web service, I had to verify the account to use the method provided by web service. First, I thought of attaching the account information to the SOAP header, however, other colleagues in the company are using non.. Net program call found a lot of inconvenience. Another Google town found a session method, which is an example we saw when we looked for "multipart upload large attachments by web service, it is no wonder that the expected results are not found during web service verification. The code is relatively simple. The main code is as follows:
View plaincopy to clipboardprint?
/// <Summary>
/// Authorization verification: This method is called when you call web service. After the call is completed, it is like a normal website login. As long as the session does not time out, you do not need to call this method again.
/// </Summary>
/// <Param name = "appname"> program name </param>
/// <Param name = "appauthorizecode"> authorization code </param>
/// <Returns> </returns>
[Webmethod (enablesession = true, messagename = "Authorization Verification")]
Public bool checkauthorize (string appname, string appauthorizecode)
{
If (appname = "account name" & appauthorizecode = "123456 ")
Session ["login"] = true;
Else
Session ["login"] = false;
Return (bool) session ["login"];
}
/// <Summary>
/// Add the file and then call
/// </Summary>
/// <Param name = "model"> archive object class </param>
/// <Returns> </returns>
[Webmethod (enablesession = true, messagename = "add file")]
Public String addarchive (model. Archives Model)
{
Try
{
If (session ["login"]! = NULL & session ["login"]. Equals (true) // you can check whether the session value has been verified. Before each method, you must determine
{
// The following code is the sample code. You can place your own code as needed.
Bll. Archives BLL = new BLL. Archives (); // instantiate the archive operation class
If (BLL. addarchive (model) // Add a file
Return "file added successfully ";
Else
Return "failed to add file ";
}
Else
Return "Verification Failed ";
}
Catch (exception ERR)
{
Return err. message;
}
}
/// <Summary>
/// Authorization verification: This method is called when you call web service. After the call is completed, it is like a normal website login. As long as the session does not time out, you do not need to call this method again.
/// </Summary>
/// <Param name = "appname"> program name </param>
/// <Param name = "appauthorizecode"> authorization code </param>
/// <Returns> </returns>
[Webmethod (enablesession = true, messagename = "Authorization Verification")]
Public bool checkauthorize (string appname, string appauthorizecode)
{
If (appname = "account name" & appauthorizecode = "123456 ")
Session ["login"] = true;
Else
Session ["login"] = false;
Return (bool) session ["login"];
}
/// <Summary>
/// Add the file and then call
/// </Summary>
/// <Param name = "model"> archive object class </param>
/// <Returns> </returns>
[Webmethod (enablesession = true, messagename = "add file")]
Public String addarchive (model. Archives Model)
{
Try
{
If (session ["login"]! = NULL & session ["login"]. Equals (true) // you can check whether the session value has been verified. Before each method, you must determine
{
// The following code is the sample code. You can place your own code as needed.
Bll. Archives BLL = new BLL. Archives (); // instantiate the archive operation class
If (BLL. addarchive (model) // Add a file
Return "file added successfully ";
Else
Return "failed to add file ";
}
Else
Return "Verification Failed ";
}
Catch (exception ERR)
{
Return err. message;
}
} We can see that the "enablesession = true" attribute is used for session verification.