2016-01-06 28 views
0

我有一個Web方法的Web服務:防止響應,如果沒有cookie的C#

namespace Temp 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService] 
    public class Init : System.Web.Services.WebService 
    { 
    [WebMethod(EnableSession = true)] 
    public string GetData(string userCode) 
    { 
    // code here 
    } 
    } 
} 

我想從沒有cookie的要求,以防止含有「會話ID」來獲得響應(或發送輸入反應40X )。 餅乾:

從我的系統的要求已經與如cookie發送ASP.NET_SessionId =

如果我對這個Web方法的請求,並從cookie中刪除會話ID,我得到的數據從服務器。

我該如何預防這種情況?

回答

0

你需要做這樣的事情

var sessionCookie = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"]; 
if(sessionCookie == null || String.IsNullOrWhiteSpace(sessionCookie.Value) 
    return null; // or throw WebFaultException(httpStatusCode) 
+0

但如果我有50個Web方法?沒有更多的一般我可以做的? – user5753198

+0

你可以把相同的片段放在服務類的構造函數中。 (需要無參數而無需做額外的工作) –