Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC FormsAuthentication check if user logged in

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?

like image 403
retif Avatar asked May 30 '15 14:05

retif


People also ask

Where do I find forms authentication in MVC?

The Forms Authentication is available in System.Web.Security namespace. In order to implement the Forms Authentication in the ASP.NET MVC application, we need to do the following three things

How do I log out of the forms authentication session?

Rename the existing WebForm1.aspx page as Default.aspx, and open it in the editor. This button is used to log off from the forms authentication session. Switch to Design view, and save the page. Double-click SignOut to open the code-behind page (Default.aspx.cs), and copy the following code in the cmdSignOut_ServerClick event handler:

How do I use ASP NET authentication in Visual Studio?

Visual Studio .NET Open Visual Studio .NET. Create a new ASP.NET Web application, and specify the name and location. This section demonstrates how to add and modify the <authentication> and <authorization> configuration sections to configure the ASP.NET application to use forms-based authentication.

What is login URL and authorize attribute in MVC?

The login URL is nothing but the URL to which the unauthenticated user navigates and in this case, this is nothing but the Login action method of the Accounts Controller. The Authorize Attribute is the built-in attribute provided by MVC which is basically used to authenticate a user.


1 Answers

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

@if (User.Identity.IsAuthenticated) { /* content */}
like image 149
Techy Avatar answered Sep 20 '22 05:09

Techy