6

I want to show some div at the view only if user has logged in.

That's how I tried to do it:

@{
    if (Request.IsAuthenticated)
    // if (User.Identity.IsAuthenticated)
    {
        <div>
            Some content only for logged in users.
        </div>
    }
}

But Request.IsAuthenticated (and User.Identity.IsAuthenticated) is always true, even in the very beginning, right after I start the website from Visual Studio. Apparently, it gets me as the user logged in Windows (because User.Identity.Name returns my Windows login), but I need it to check if user has authenticated on website via FormsAuthentication.

That's my web.config:

<authentication mode="Forms">
  <forms loginUrl="/Account/Login" timeout="2880" />
</authentication>

How do I check if users has logged in via FormsAuthentication?

1
  • For now I did it like that: if (Request.Cookies[FormsAuthentication.FormsCookieName] != null), but that doesn't seem to be the right way. Commented May 30, 2015 at 14:12

1 Answer 1

8

If you mean that you want to check if the user has logged in, I use the following:

@if (User.Identity.IsAuthenticated) { /* content */}
Sign up to request clarification or add additional context in comments.

6 Comments

Nope, tried that, it always returns true as well.
are you using individual user accounts, organizational, or windows authentication?
Where? In web.config there are only <authentication mode="Forms"> lines that I mentioned in the post.
When you first create your application you get to choose the type. please see the following, this might solve your problem: forums.asp.net/t/…
Well, I chose None, and this selection, when creating new project, only affects <authentication mode> in web.config.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.