简体   繁体   English

在ASP.NET C中调用Web服务时,Session为null

[英]Session is null when calling a web service in ASP.NET C#

I have a Login Class which has a function: isCorrect() that takes username and password as two attributes And a asp.net WebService To allow using AJAX. 我有一个Login类,它有一个函数:isCorrect(),它将用户名和密码作为两个属性和一个asp.net WebService来允许使用AJAX。

LoginService.cs LoginService.cs

public Login CorrectLogin(string username, string password) 
{   
   Login thisLogin = Login.isCorrect(username, password);
   int thisLoggedinUserID = thisLogin.LoggedinUserID;

   if (thisLoggedinUserID != 0)
   {
      Session["loggedinUser"] = thisLoggedinUserID;
   }

   return thisLogin;
}

When I want to set value of Session["loggedinUser"] = thisLoggedinUserID this error accrues: 当我想设置Session["loggedinUser"] = thisLoggedinUserID值时,此错误累积:

Object reference not set to an instance of an object.

I can't understand what is solution. 我无法理解什么是解决方案。

Web services don't have Session by default. 默认情况下,Web服务没有Session。 Add an attribute to the WebMethod.. 将属性添加到WebMethod ..

 [WebMethod(EnableSession=true)]
 public Login CurrentLogin .....

Looks like the Session object is null. 看起来Session对象为null。 Are you sure sessions are switched on in your application? 您确定应用程序中的会话已打开吗?

Here is a good tutorial on login and access 这是一个关于登录和访问的好教程

http://www.mikesdotnetting.com/Article/75/Simple-Login-and-Redirect-for-ASP.NET-and-Access http://www.mikesdotnetting.com/Article/75/Simple-Login-and-Redirect-for-ASP.NET-and-Access

Its not hard to find, i guess your making your own provider. 它不难找到,我想你自己的提供者。 codeplex etc has a bunch too. codeplex等也有一堆。

Sessions doesn't work well in Web Services c#. 会话在Web服务c#中不能很好地工作。 Few months ago I would do the same as you, but I change the system. 几个月前,我会像你一样做,但我改变了系统。 Now I'm using a token identification (using a persistence layer): First you must login, and every time you'll make a call, you must send the token, in one parameter or a soap header or something like this. 现在我正在使用令牌识别(使用持久层):首先你必须登录,每次你打电话,你必须在一个参数或肥皂头或类似的东西中发送令牌。 I Recommend to use SSL always. 我建议始终使用SSL。

Another option is to ident every web service call with login and password in a SOAPHeader, and use a SOAPExtension to autenticate or not the user-call. 另一种选择是在SOAPHeader中使用登录名和密码识别每个Web服务调用,并使用SOAPExtension来验证或不是用户调用。 This method is only valid for SOAP protocol communication, not for HTTP POST or HTTP GET. 此方法仅对SOAP协议通信有效,而不适用于HTTP POST或HTTP GET。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM