How to check if user is authorized inside Action
I suggest first figuring out what kind of Authorization your using. ;)
The answer you posted is correct. From what I remember poking around the [Authorize] attribute and related ActionFilter code MVC internally calls Page.User.Identity.IsAuthenticated just like those code examples.
Request.IsAuthenticated
should work for what you're trying to do.
If you just want to know if the user is logged in:
if (User.Identity.IsAuthenticated) { ... }
If you are trying to do anything role-specific:
if (User.IsInRole("Administrators")) { ... }
The User
instance is a public property of the Controller
class, so you always have access to it from a Controller you write. If no user is logged in you should have a GenericPrincipal
for the User
and a GenericIdentity
for the User.Identity
, so don't worry about checking for nulls.