4

I have created a Login page where users must provide username and password to have access to some specific resources, where they can upload images, or just edit some description about themselves. My web.config file looks like this:

    <authentication mode="Forms">
      <forms loginUrl="Secure/Login.aspx" defaultUrl="index.aspx" name=".ASPXFORMSAUTH" timeout="30"/>
    </authentication>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>

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

So when the user has typed in the username and pw, he is redirected to the index.aspx page. Depending wether the user is logged in or not, the index.aspx should show or hide some stuff. This is how I check if he is logged in:

bool isLoggedIn = HttpContext.Current.User.Identity.IsAuthenticated;  
    if (isLoggedIn) 
    {
        placeHolder2.Visible = true;
        ...
    }

Now the problem is that the: HttpContext.Current.User.Identity.IsAuthenticated; ALWAYS returns true, so unauthorised people will be seeing the stuff that should be hidden.

I am not sure about the: HttpContext.Current.User.Identity.IsAuthenticated; I just googled "How to check if user is logged in", and suggestions were the: HttpContext.Current.User.Identity.IsAuthenticated;

I want only the people that are logged in to view the private stuff. How do I go about this? How do I make the: HttpContext.Current.User.Identity.IsAuthenticated only return true when the user is logged in? Thanks

user704988
  • 436
  • 1
  • 9
  • 24
AomSet
  • 373
  • 4
  • 5
  • 14

3 Answers3

8
if (Request.IsAuthenticated) {.....}

edit based on some comments Authenticated via "Forms", check here

HttpContext.Current.User.Identity.IsAuthenticated // will be "Forms" if using forms based auth // "Negotiate" is using windows integrated // etc

If using .net 4.5 and you wanted "SET" user claims. The ClaimsPrincipal is recommended reading

fbarikzehy
  • 4,885
  • 2
  • 33
  • 39
phil soady
  • 11,043
  • 5
  • 50
  • 95
  • `HttpContext.Current.User.Identity.IsAuthenticated` and `HttpContext.Current.Request.IsAuthenticated` are same. In other words, according to OP, using `Request.IsAuthenticated` won't make different because it will also return true. – Win Aug 01 '13 at 23:59
  • The: Request.IsAuthenticated gives the desired behavior, but the to above doesn't. Those were the ones that I already tried. And thanks, it works with: Request.IsAuthenticated – AomSet Aug 02 '13 at 00:05
  • I was wrong, it didnt fix it, still the same problem. So far I found out why it returns true, even if the user has not logged in. The: HttpContext.Current.User.Identity.IsAuthenticated returns the name on my computer (host address/name). Why is that so? I should instead check if the user has logged in with username and password – AomSet Aug 02 '13 at 00:24
3

bool isLoggedIn = System.Web.HttpContext.Current.User.Identity.IsAuthenticated

1

My coding is

bool val1 = (System.Web.HttpContext.Current.User != null) &&
    (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) &&
    (System.Web.HttpContext.Current.User.Identity.AuthenticationType.ToString() == "Forms");

for identifying the users with domain login and logged in form

David Gorsline
  • 4,933
  • 12
  • 31
  • 36
Marc Zeroc
  • 19
  • 1