0

I want to check the value of a session in my ASP.NET website (WebForms). So what I have tried to do is:

// This file called 'Encryptor.cs' and is located under the 'App_Code' folder.

using System;
using System.Data;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.Text;

public static class Encryptor
{
    public static bool CheckLoginStatus()
    { 
        bool LoginStatus = false;

        if ((string)Session["username"] == null)
        { 
             LoginStatus = true;
        }

        return LoginStatus;
    }
}

I keep getting this message: "The name 'Session' does not exist in the current context." How do I fix it?

Ido
  • 397
  • 2
  • 7
  • 22
  • 1
    Check out this post: [How to access session variables from any class in ASP.NET?](http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net) The accepted answer is pretty good. – scheien Jan 16 '14 at 18:18

1 Answers1

1

You should be able to access the Session variable like this:

string username = (string)System.Web.HttpContext.Current.Session["username"]; 

The HttpContext resides in the System.Web namespace. You could either reference the namespace via a using System.Web, or go full namespace when accessing the session.

If you want to expand the usefulness of Session, and have strong typing on the variables, then you could opt in for this solution: How to access session variables from any class in ASP.NET?

Community
  • 1
  • 1
scheien
  • 2,457
  • 1
  • 19
  • 22
  • But now it says: "The name 'HttpContext' does not exist in the current context." || edit: I should have added System.Web before what you've suggested. Now it works. – Ido Jan 16 '14 at 18:21
  • 2
    You need to reference the whole namespace. Either add `using System.Web` at the top, or `System.Web.HttpContext.Current.Session["username"]` – scheien Jan 16 '14 at 18:22