Checking for logged in user using jQuery in ASP.NET

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 16553 red flag
Rating: 4.5 out of 5  
 2 vote(s)

In this article, we shall learn how to check if a user is logged into the application using jQuery to perform some client side operation.
Read Forms Authentication in ASP.NET with C#: Basic before this article.


There was a Forums posts asking for how to check for the user Session, if a user is logged in. Even if there is no need to check for Session object to ensure that user is logged in as it can be done using alternative better way. However, I thought lets write an article for this as this is very frequently asked question in the Forums.


The objective of this article is to check if a user is logged in using client side script (JavaScript/jQuery) before performing some member specific article in the browser.

Page that show if user is logged in

Here is my simple page that checks if user is logged in and shows a true/false alert based on user's logged in status.

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
     <script src=""></script>

      <script type="text/javascript">
          $.get("test.aspx?checkForLogin=1", "", function (result) {
            // if true, perform member specific operation otherwise show appropriate message

    <form id="form1" runat="server">
In the above code snippet; as soon as page loads, a ajax get request is sent to test.aspx page with a querystring (checkForLogin) and whatever is returned form the test.aspx is shows as alert message to the browser.

Checking for user log in and returning status

In the test.aspx.cs page, I have following code snippet in the Page_Load event
Note that my test.aspx.cs page is completely blank except the Page directive.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
    protected void Page_Load(object sender, EventArgs e)
        if (!string.IsNullOrEmpty(Request.QueryString["checkForLogin"]))
            if (User.Identity.IsAuthenticated)

In the above code snippet, I am checking for the checkForLogin querystring and checking if user is authenticated using User.Identity.IsAuthenticated property and returning true, if yes otherwise false if no.

The same value is returned as result to the page from where test.aspx page is being called.

The querystring checkForLogin was not required but we have kept it as we wanted to use the .aspx page for other purpose also. If the purpose of test.aspx is only to determine if your is logged in, we can remove the querystring condition.

Point of Interest - Correct way to implement Login functionality

It is a general practice by beginner developer to set a Session value if the username and password match in the database and check for that Session value on every page to ensure that user is logged in. Even if this process solve its purpose but this is not the right way and it is prone to error if the application is kept idle for sometime and session expires. The correct way to implement login functionality in ASP.NET is explained in this article "Forms Authentication in ASP.NET with C#: Basic"


In this article, we learnt how to check the user login status using jQuery ajax method without refreshing the page and also understood the best way to implement login functionality in ASP.NET.

Thanks for reading the article, do subscribe for RSS feed to get new article notification in Inbox.
Read Forms Authentication in ASP.NET with C#: Advance after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Anke99 on: 10/9/2018 | Points: 25
This is a valuable point which is very remarkable and point to point you need to get it and then apply in your working method how we can add it in our computer system.

Login to post response

Comment using Facebook(Author doesn't get notification)