Anoopkumar
Anoopkumar

Reputation: 106

How to check ASP.NET C# Request.IsAuthenticated in Javascript?

At the page load I want to check Request.IsAuthenticated. How can I do it through javascript. The code below shows how I did it in asp.net c#?

if (!Request.IsAuthenticated)
{
     Response.Redirect("~/Login.aspx");
}

Upvotes: 1

Views: 5032

Answers (3)

zinczinc
zinczinc

Reputation: 554

This should work:

    <script type="text/javascript">

        function foo(){
            if('@Request.IsAuthenticated' === 'True') {
                // your implementation for authenticated users go here

            }
            else{
                // your implementation for not authenticated users....
                //e.g. alert('You must be logged in to do bla bla bla');
            }
        }
    </script>

Upvotes: 1

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14216

If you are using ASP.NET MVC then you can do like following.

 <script>
     var isRequestAuthenticated=' @Request.IsAuthenticated';
 </script>

Else if you are using normal ASP.NET Web forms

 <script>
     var isRequestAuthenticated='<%=Request.IsAuthenticated%>';
 </script>

Now you can use that variable to check whether this request is authenticated or not.

Upvotes: 3

Kallum Tanton
Kallum Tanton

Reputation: 777

By JavaScript, I assume that you mean using AJAX so that the value is read directly into your JavaScript.

Response.Write(Request.IsAuthenticated ? "TRUE" : "FALSE");

This will return "TRUE" if true, "FALSE" if false. Put this into a method that only your AJAX function calls.

Upvotes: 0

Related Questions