C # Web Service non-SOAP header (Session) authentication method

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.