mhesabi
mhesabi

Reputation: 1140

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.

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:

Object reference not set to an instance of an object.

I can't understand what is solution.

Upvotes: 16

Views: 12288

Answers (4)

Neonamu
Neonamu

Reputation: 736

Sessions doesn't work well in Web Services 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.

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. This method is only valid for SOAP protocol communication, not for HTTP POST or HTTP GET.

Hope this helps.

Upvotes: 0

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8444

Web services don't have Session by default. Add an attribute to the WebMethod..

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

Upvotes: 37

Michael Hahn
Michael Hahn

Reputation: 143

Here is a good tutorial on login 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.

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102763

Looks like the Session object is null. Are you sure sessions are switched on in your application?

Upvotes: 1

Related Questions