Is there a way to check if the current user is authenticated with javascript?

I need to determine if a user is authenticated in a javascript function before enabling him to continue.
RahnLevAsked:
Who is Participating?
 
MikeRCWattsCommented:
Any user validation which must be secure needs to be done server side, since a user can get round anything in the client side Javascript.

..if you just want simple different behaviour on the page depending whether the user is logged in, but don't care if hackers get round it, you could simply create a flag from the back end, e.g.

<script lang-javascript>
var isInRole = true;  //only write this from the server if logged in, else write false.
</script>
and use that in your Javascript.    .. backed up by validation server side.

However, if you are going to generate different page content from the server depending whether the user is logged in, you may as well do it 'properly' and generate different pages depending if they are logged in?

0
 
OriNetworksCommented:
authenticated against what?
0
 
yuniel537Commented:
I recommend you that before you run the funtion just ask for the credentials store it in a var and then check it inside the funtion, I don't really know if I did understand you question but I hope this can help you, it help me, before
http://www.hotscripts.com/category/javascript/scripts-programs/user-authentication/  
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
BlomholmCommented:
Having user authentication with javascript is pointless if you want security, since JavaScript is client-side meaning anyone could bypass it with ease.
however, if security is not an issue, then yes you can.
0
 
Anurag ThakurTechnical ManagerCommented:
Client-Side scripts cannot access Server-Side variables! Hence you cannot access the server-side session object from your javascript.

I'd suggest that instead of savig that value to a session save it to a hidden input field, so that the JavaScript can read the value from it.
0
 
fsze88Commented:
checking javascript enabled/disabled?
please try to set cookies by javascript http://www.w3schools.com/JS/js_cookies.asp
and read back by server side scripting e.g. php, asp even jsp
http://www.w3schools.com/ASP/asp_cookies.asp
http://www.w3schools.com/PHP/php_cookies.asp
server side scripting can determine is javascript enabled? by cookies values. if null or empty string... javascript disabled, else should enabled..

you could use NOSCRIPT tag to alert users "javascript not enabled" or redirect them to another page

<NOSCRIPT>
    <meta http-equiv=REFRESH content=0;URL={target url}>
  </NOSCRIPT>
0
 
RahnLevAuthor Commented:
I need to check if the user is logged in or a guest like:

HttpContext.Current.User.IsInRole
0
 
fsze88Commented:
??
you could check cookies by asp.net && not much concern javascript's cookies.....????
If you must check cookies by javascript got it null or "" using noscript tag to do what you want....

<NOSCRIPT>
    Sorry, this page is not accessible without enabled javascript....
  </NOSCRIPT>
<NOSCRIPT>
    <meta http-equiv=REFRESH content=0;URL={target url}>
  </NOSCRIPT>
0
 
Anurag ThakurTechnical ManagerCommented:
i am going to repeat the same thing what i said previously

Client-Side scripts cannot access Server-Side variables! Hence you cannot access the server-side session object from your javascript.
I'd suggest that instead of savig that value to a session save it to a hidden input field or a hidden web control, so that the JavaScript can read the value from it.

on page load you can save the value for HttpContext.Current.User.IsInRole to the hidden control and access it at client side and take action for it
0
 
RahnLevAuthor Commented:
This is what I did looks, its working, but seem to be a little bit clumsy.
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
        xmlDoc.Load(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
        if (xmlDoc.ChildNodes[1].FirstChild.ChildNodes[1].OuterXml.Contains("ÔÔÖÞàÕê éÜÙ"))
        {
            if (HttpContext.Current.User.IsInRole("Clients"))
            {
                xmlDoc.Load(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
                System.Xml.XmlNode node = xmlDoc.ChildNodes[1].FirstChild.ChildNodes[1];
                node.Attributes[0].Value = "~/CategoriesLoggedIn.aspx";
                xmlDoc.Save(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
            }
            else
            {
                xmlDoc.Load(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
                System.Xml.XmlNode node = xmlDoc.ChildNodes[1].FirstChild.ChildNodes[1];
                node.Attributes[0].Value = "~/Categories.aspx";
                xmlDoc.Save(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
            }
        }
        if (xmlDoc.ChildNodes[1].FirstChild.ChildNodes[3].OuterXml.Contains("àÙÔÕÜ ×éÑÕß"))
        {
            if (HttpContext.Current.User.IsInRole("Clients"))
            {
                xmlDoc.Load(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
                System.Xml.XmlNode node = xmlDoc.ChildNodes[1].FirstChild.ChildNodes[3];
                node.Attributes[0].Value = "~/AccountManagmentLoggedIn.aspx";
                xmlDoc.Save(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
            }
            else
            {
                xmlDoc.Load(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
                System.Xml.XmlNode node = xmlDoc.ChildNodes[1].FirstChild.ChildNodes[3];
                node.Attributes[0].Value = "~/AccountManagment.aspx";
                xmlDoc.Save(@"C:\Documents and Settings\Rahn\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer\Copy_5_of El-Hatavor\Web.sitemap");
            }
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.