views:

64

answers:

3

Usually I protect my Actions with [Authorize] but this time I need to check if a user is authorized inside the action.

Eg

if(userIsAuthorized) {
    //do stuff
}
else {
    //return to login page
}

I believe I am using 'Forms Authentication'

This question is kind of similar to this but none of the answers given seemed to work.

EDIT: I have done some more digging- it seems if I breakpoint on an Action that has [Authorize], the User.Identity is set, but on Actions without it, the User.Identity is empty, even if I am logged in

+1  A: 

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.

jfar
Double checked and it is Forms Authentication
elwyn
+1  A: 

Request.IsAuthenticated should work for what you're trying to do.

Esteban Araya
If I do that on an Action decorated with `[Authorize]` it works fine, however if I do that on this Action (not decorated with [Authorize]) it is always false, regardless of whether I am logged in or not.
elwyn
+2  A: 

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.

Aaronaught
Again, only gives me 'true' if used within an `[Authorize]`'d Action
elwyn
@elwyn: I don't believe that's correct. I just tested it here on an action without the `[Authorize]` attribute and `User.Identity.IsAuthenticated` is `true`. Are you sure that the session is actually logged in when you are testing this?
Aaronaught
@Aaronaught Yes, just double (triple) checked, definantly logged in while trying that, and still see false
elwyn
-----In my non-[Authorize]'d Action:User.Identity{System.Security.Principal.GenericIdentity} [System.Security.Principal.GenericIdentity]: {System.Security.Principal.GenericIdentity} AuthenticationType: "" IsAuthenticated: false Name: ""-----In an Authorized one:User.Identity{System.Web.Security.FormsIdentity} [System.Web.Security.FormsIdentity]: {System.Web.Security.FormsIdentity} AuthenticationType: "Forms" IsAuthenticated: true Name: "admin"
elwyn
Sorry for lack of formatting, doesn't look like I can use tags in comments :/
elwyn
@elwyin: Nothing I can do reproduces the behaviour that you seem to be seeing. You do not need the `[Authorize]` attribute for `User` and `User.Identity` to be valid. Do you have any other attributes on the Controller? Have you tried doing this in a new, clean MVC project, to make sure that nothing else in your app is interfering?
Aaronaught