如果没有cookie,则阻止响应c#

时间:2016-01-06 17:05:35

标签: c# web-services cookies

我使用网络方法提供网络服务:

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
    }
  }
}

我想阻止没有包含" session-id"的cookie的请求得到回应(或发送反响40x)。

我系统发出的请求已经发送,如: Cookie:ASP.NET_SessionId =

如果我向此Web方法发出请求并从cookie中删除session-id,我会从服务器获取数据。

我该如何预防这种情况?

1 个答案:

答案 0 :(得分:0)

你需要做这样的事情

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