16

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.

2
  • Can you post the exception stack trace? Commented Mar 22, 2012 at 20:39
  • 1
    since its error happens on webservice class only stacktrace I found is : at LoginService.CurrectLogin(String username, String password) in f:\...\App_Code\LoginService.cs:line 19 Commented Mar 22, 2012 at 21:01

4 Answers 4

37

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

 [WebMethod(EnableSession=true)]
 public Login CurrentLogin .....
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

how can I know if its on? for example this is my web.config <sessionState timeout="60" mode="StateServer" stateNetworkTimeout="20"></sessionState>
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.