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
?
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
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:
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.
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.
If you mean that you want to check if the user has logged in, I use the following:
@if (User.Identity.IsAuthenticated) { /* content */}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With