Skip to main content
     


How Do I know if a user is logged in for an ASP.NET website?

Also:

How to Access User.Identity.IsAuthenticated From User Control

Asked July 27, 2014
Posted Under: .NET
998 views
3 Answers

The page class has immediate access to this, you'll have to use httpcontext to get the information while in the user control as such:
dim isAuthenticated as Boolean

isAthenticated= HttpContext.Current.User.Identity.IsAuthenticated

if isAuthenticated = false then
page.redirect("another_aspnet_page.aspx")
end if

Answered July 27, 2014
If you are checking in code behind after pressing a button, the authentication code has has executed yet.
Best solution is to check on loading the first and then disabling the button if not authenticated.

Answered July 27, 2014
You can also use a web service, checked with javascript before pressing the button.
using System;

using System;

using System.Web.Services;

using System.Web.Services.Protocols;



public class Authentication : SoapHeader {

public string User;

public string Password;

}



public class SecureWebService : WebService{

public Authentication authentication;



[WebMethod]

[SoapHeader("authentication")]

public string ValidUser(){

if (User.IsInRole("Customer"))

return "User is in role customer";



if (User.Identity.IsAuthenticated)

return "User is a valid user";

return "not authenticated";

}

}

Answered July 27, 2014
Your Answer