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?
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.
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.
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.
Using the Codebool isAuthenticated = Request. IsAuthenticated; the result is always true . It's not what we expect when signout is performed.
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
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