C# Web Service非Soap頭(Session)驗證方法

來源:互聯網
上載者:User

最近在做一個程式的Web Service,需要驗證帳號才能使用Web Service提供的方法,首先想到把帳號資訊附加在Soap頭中傳過去的方式,但在公司其他同事在使用非.net程式調用的時候發現有諸多不便。又Google了一鎮子,發現一個使用Session的方式,說起來還是找“Web Service分段上傳大附件”的時候看到的一個樣本,難怪以前找Web Service驗證的時候老找不到想要的結果。代碼比較簡單,主要代碼如下:
view plaincopy to clipboardprint?
/// <summary>   
 /// 授權驗證,在調用Web Service的時候先調用這個方法,調用完成後就像普通網站登入一樣,只要Session不逾時就不需要再次調用此方法了   
 /// </summary>   
 /// <param name="appName">程式名稱</param>   
 /// <param name="appAuthorizeCode">授權碼</param>   
 /// <returns></returns>   
 [WebMethod(EnableSession = true, MessageName = "授權驗證")]   
 public bool CheckAuthorize(string appName, string appAuthorizeCode)   
 {   
 if (appName == "帳號名稱" && appAuthorizeCode == "123456")   
 Session["Login"] = true;   
 else  
 Session["Login"] = false;   
  
 return (bool)Session["Login"];   
 }   
  
 /// <summary>   
 /// 添加檔案,然後再調用   
 /// </summary>   
 /// <param name="model">檔案實體類</param>   
 /// <returns></returns>   
 [WebMethod(EnableSession=true,MessageName="添加檔案")]   
 public string AddArchive(Model.Archives model)   
 {   
 try  
 {   
  
 if (Session["Login"] != null && Session["Login"].Equals(true))  //這裡就是判斷Session值,即有沒有通過驗證。每個方法前都需要判斷下   
 {   
 //以下代碼為範例程式碼,可以根據需要放置自己的代碼了   
 BLL.Archives bll = new BLL.Archives(); //檔案操作類的執行個體化   
  
 if (bll.AddArchive(model)) //添加檔案   
 return "檔案添加成功";   
 else  
 return "檔案添加失敗";   
 }   
 else  
 return "未通過驗證";   
 }   
 catch (Exception err)   
 {   
 return err.Message;   
 }   
 } 
/// <summary>
 /// 授權驗證,在調用Web Service的時候先調用這個方法,調用完成後就像普通網站登入一樣,只要Session不逾時就不需要再次調用此方法了
 /// </summary>
 /// <param name="appName">程式名稱</param>
 /// <param name="appAuthorizeCode">授權碼</param>
 /// <returns></returns>
 [WebMethod(EnableSession = true, MessageName = "授權驗證")]
 public bool CheckAuthorize(string appName, string appAuthorizeCode)
 {
 if (appName == "帳號名稱" && appAuthorizeCode == "123456")
 Session["Login"] = true;
 else
 Session["Login"] = false;

 return (bool)Session["Login"];
 }

 /// <summary>
 /// 添加檔案,然後再調用
 /// </summary>
 /// <param name="model">檔案實體類</param>
 /// <returns></returns>
 [WebMethod(EnableSession=true,MessageName="添加檔案")]
 public string AddArchive(Model.Archives model)
 {
 try
 {

 if (Session["Login"] != null && Session["Login"].Equals(true))  //這裡就是判斷Session值,即有沒有通過驗證。每個方法前都需要判斷下
 {
 //以下代碼為範例程式碼,可以根據需要放置自己的代碼了
 BLL.Archives bll = new BLL.Archives(); //檔案操作類的執行個體化

 if (bll.AddArchive(model)) //添加檔案
 return "檔案添加成功";
 else
 return "檔案添加失敗";
 }
 else
 return "未通過驗證";
 }
 catch (Exception err)
 {
 return err.Message;
 }
 }  可以看到使用Session的方式來驗證主要還是在於“EnableSession = true”這個屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.