uj.
uj.

Reputation: 11

asp.net: check whether session is valid

how to check whether users is authenticated and session is valid on pages after say 30 mins.

Upvotes: 1

Views: 5799

Answers (3)

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

Assuming you are either hooking into the standard ASP.NET membership providers or using Basic/Digest authentication in IIS then you can easily tell if a user is authenticated using:

if (Request.IsAuthenticated)
{
  // User is authenticated, allow them to do things
}

If their authentication token has expired (defaults to 20 minutes with Forms Auth, Windows auth should re-authenticate correctly with each request), then the next time you check that, IsAuthenticated will return false.

You could store a token in the users session that maps back to their user account (either a hash of their user name, or their user id or similar), and then check the two on the request - if they don't match, then the session has become invalid:

// Get the current user, and store their ID in session
MembershipUser user = Membership.GetUser();
Session["UserId"] = user.ProviderUserKey;

To check this:

if (null != Session["UserId"]) {
  MembershipUser user = Membership.GetUser();

  if (Session["UserId"] == user.ProviderUserKey) {
    // User and session are valid
  }
}

But to be honest, it depends on what you are trying to do.

If you want to restrict access to certain areas of your website if the user isn't logged in, then there are mechanisms in the configuration that allow for that:

In your web.config you can add lines like the following:

<location path="SecureDirectory">
  <system.web>
     <authorization>
        <deny users="?"/>
     </authorization>
  </system.web>
</location>

This will deny all anonymous users access to the directory /SecureDirectory/ and all content below it, and direct them instead to your configured login page - for more information on the Authorization element, see "How to: Configure Directories Using Location Settings".

Upvotes: 2

Ray
Ray

Reputation: 192216

In the start of a session, you can store some key value in session state via the Global.asax:

void Session_Start(object sender, EventArgs e) 
{
    Session["userId"] = userId;  // obtained from a data source or some other unique value, etc.
}

Whenever a user makes a page request or postback, on page load of any or all your pages, check if session value is null:

protected void Page_Load(object sender, EventArgs e)
{ 
    if(Session["userId"] == null)
    {
        Response.Redirect("logout.aspx");
    }
    // do other stuff
}

If it is, then the session has expired and you can redirect then to logout page or whatever. The timeout interval is defined in your web.config file.

Upvotes: 0

Abdul Munim
Abdul Munim

Reputation: 19217

You have to make the session expire after certain time.

So, there is a section in your web.config or you have to add the section in <system.web />

Put this section inside:

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424"
  stateNetworkTimeout="10" cookieless="false" timeout="30" />

If you notice we are using InProc mode and timeout to be 30

Now its all up to you.

Add a key in Session object when you can find in any WebForm page.

public void btnLogin(object sender, EventArgs e) {
    if (validUser) {
        Session["authenticated"] = true;
    }
}

and check Session["authenticated"] when required.

Session object will be expired in 30 minutes of session instantiation.

Hope this help. Please feel free to leave me a comment if you face trouble.

Upvotes: 0

Related Questions