3

In an asp.net mvc application, i would like to use a rest webservice to return the username associated with the session id passed as a parameter.

How do I get the sessionID of the logged in User ?

Do I need to set the session ID Manually in my login method ?

Also how can I get the user information using the session ID ?

Any help will be appreciated. Thank you !

Login Method in Account Controller:

[HttpPost]
    public ActionResult LogIn(UserLoginView ULV, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            UserManager UM = new UserManager();
            string password = UM.GetUserPassword(ULV.EmailID);

            if (string.IsNullOrEmpty(password))
                ModelState.AddModelError("", "*The Email-ID or Password provided is incorrect");
            else
            {
                if (ULV.Password.Equals(password))
                {
                    FormsAuthentication.SetAuthCookie(ULV.EmailID, false);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "*The Password provided is incorrect");
                }
            }
        }

        return View();
    }

Web service method in User Controller:

 public class UserController : ApiController
    {
    [HttpGet]
    public string UserInfo()
    {
       HttpSessionState sessionValue = HttpContext.Current.Session;

        return sessionValue.SessionID.ToString();

    }
}
Anuja Sawant
  • 75
  • 1
  • 1
  • 9
  • Yes. I just confirmed that the solution [here](http://stackoverflow.com/a/27276358/1007608) will fix your problem. Very cool. – John Verco Mar 23 '17 at 16:53

2 Answers2

7

What version of .NET are you using? Using .NET Framework 4.5.2 I was able to obtain the SessionID value using: string sessionID = HttpContext.Session.SessionID;

John Verco
  • 1,357
  • 9
  • 6
  • I am using the same version. Still i get a null reference exception on the sessionID line. – Anuja Sawant Mar 23 '17 at 16:11
  • I see. I also get a NullReferenceException when trying to obtain SessionID from `HttpContext.Current.Session` in a WebAPI controller method because Session is NULL. This is not happening in the MVC page controller methods. And in the MVC controller method `HttpContext.Current` doesn't seem to exist. I would have to use `HttpContext.Session` in MVC controller methods. I just read a post that states that WebAPI methods are session-less. [link](http://stackoverflow.com/questions/11478244/asp-net-web-api-session-or-something) – John Verco Mar 23 '17 at 16:42
  • Try reading about enabling session in WebAPI here at this Stack Overflow post: [.NET Web API + Session Timeout](http://stackoverflow.com/questions/27275974/net-web-api-session-timeout?noredirect=1&lq=1) – John Verco Mar 23 '17 at 16:46
  • I got the solution. [link](http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api) turned out ot be useful ! – Anuja Sawant Mar 23 '17 at 16:55
  • Thank you very much for your time and help @John – Anuja Sawant Mar 23 '17 at 16:57
1

link The first answer in this link gives the solution to obtain the session ID.

Community
  • 1
  • 1
Anuja Sawant
  • 75
  • 1
  • 1
  • 9