[c#] How to check user is "logged in"?

I am using form authentication with below method in my ASP.NET application

FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);

How do I check whether user is logged in or not? And how can I get the user name of a logged in user?

This question is related to c# asp.net forms-authentication

The answer is


if (User.Identity.IsAuthenticated)
{
    Page.Title = "Home page for " + User.Identity.Name;
}
else
{
    Page.Title = "Home page for guest user.";
}

The simplest way:

if (Request.IsAuthenticated) ...

Easiest way to check if they are authenticated is Request.User.IsAuthenticated I think (from memory)


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to forms-authentication

How to get the cookie value in asp.net website How to check user is "logged in"? ASP.NET Forms Authentication failed for the request. Reason: The ticket supplied has expired ASP.NET MVC - Set custom IIdentity or IPrincipal Why is <deny users="?" /> included in the following example? FormsAuthentication.SignOut() does not log the user out How to get the current user in ASP.NET MVC