C # WEB Service non-SOAP header (session) authentication method

Source: Internet
Author: User
Tags bool header soap

A recent Web service for a program that needs to validate an account to use the method provided by the Web service first thought of attaching the account information to the SOAP header, but found a lot of inconvenience when other colleagues in the company were using a non-. NET program call. Another Google a town, found a way to use the session, said or to find "Web service segment upload large attachment" When you see an example, no wonder that the previous search for Web service verification when the old find the results. The code is relatively simple, the main code is as follows:

<summary>///Authorization Verification, call this method when calling the Web service, after the call is completed, like a normal web site login, as long as the session does not timeout does 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 = "Authorized Authentication")] public bool Checkauthorize (stri Ng AppName, String Appauthorizecode {if (AppName = = "account name" && Appauthorizecode = = "123456") session["Login"] 
= true; 
else session["Login" = false; 
return (BOOL) session["Login"]; ///<summary>///Add files, and then call///</summary>///<param name= "model" > Archive entity class </param>///<r eturns></returns> [WebMethod (enablesession=true,messagename= "Add File")] public string addarchive ( Model.archives model) {try {if (session["login"]!= null && session["Login". Equals (TRUE)//Here is the judgment session value, that is, there is no validation. Each method needs to be judged before {//The following code is sample code, you can place your own code as needed BLL. Archives BLL = new BLL. Archives (); File drillAn instantiated if (BLL) for the class. 
Addarchive (model))//Add File Return "file add Success"; 
else return "file add failed"; 
else return "failed validation"; catch (Exception err) {return err. 
message; }///<summary>///authorization Authentication, call this method first when calling the Web service, and after the call is complete like a regular web site login, you do not need to call this method again as long as the session does not timeout///</summary >///<param name= "AppName" > Program name </param>///<param name= "Appauthorizecode" > Authorization Code </param>/ <returns></returns> [WebMethod (EnableSession = true, MessageName = "Authorized Authentication")] 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 files, and then call///</summary>///<param name= "model" > Archive entity class </param>///<retur ns></returns> [WebMethod (enablesession=true,messagename= "Add File")] public string addarchive (model.archives Model) {try {if (session["Login"]!= null && session["Login"]. Equals (TRUE)//Here is the judgment session value, that is, there is no validation. Each method needs to be judged before {//The following code is sample code, you can place your own code as needed BLL. Archives BLL = new BLL. Archives (); Instantiation of the file Operation class if (BLL.
Addarchive (model))//Add File Return "file add Success";
else return "file add failed";
else return "failed validation"; catch (Exception err) {return err.
message; }
}

You can see how the session is used to verify that the main or the "EnableSession = True" attribute.

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.