Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.IsAuthenticated function in ASP.NET 5

Tags:

asp.net-core

Is there an equivalent to Request.IsAuthenticated in ASP.NET 5 hidden somewhere or are we expected to loop through the user's identities and determine this ourselves?

like image 324
noodles Avatar asked Jun 03 '15 23:06

noodles


People also ask

Is authenticated .NET core?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.

What is IsAuthenticated?

Definition. The IsAuthenticated property is a boolean value that indicates whether the current user is authenticated (logged in). The property value is a boolean true if the current user is authenticated, otherwise false.

How does request IsAuthenticated work?

Identity with a new IIdentity object that will return true from its IsAuthenticated property. Request. IsAuthenticated will then return true . In the case of Forms authentication, the forms authentication module uses the encrypted authentication ticket contained in the authentication cookie to authenticate the user.

How do I make request IsAuthenticated true?

Using the Codebool isAuthenticated = Request. IsAuthenticated; the result is always true . It's not what we expect when signout is performed.


1 Answers

If you just need to know if the User object is authenticated, this property should do the trick:

User.Identity.IsAuthenticated 

If you need to prevent an action from being called by an unauthenticated user, the following attribute class works great.

public class BasicAuthAttribute : ActionFilterAttribute, IAuthenticationFilter {     public void OnAuthentication(AuthenticationContext filterContext)     {     }      public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)     {         var user = filterContext.HttpContext.User;         if (user == null || !user.Identity.IsAuthenticated)         {             filterContext.Result = new HttpUnauthorizedResult();         }     } } 

I use this in my base controller class as follows.

[BasicAuth] public abstract class BaseAuthorizedController : Controller 
like image 74
Martin Noreke Avatar answered Oct 01 '22 11:10

Martin Noreke