I extended AuthorizeAttribute
with my own class.
I specifically override the OnAuthorization
method:
- I create a
ClaimsPrincipal
(if user is recognized) and ...- assign the
ClaimsPrincipal
tofilterContext.HttpContext.User
- and if
this.Users
IsNullOrEmpty I setthis.Users
to theClaimsPrincipal.Identity.Name
- assign the
- ...and I finish with a call to
base.OnAuthorization
I use this AuthorizeAttribute extension by adding an instance to the GlobalFilterCollection:
globalFilterCollection.Add(new AuthorizationAttributeExtension());
The app is started for the first time...
Request 1 comes in from User 1, User1 ClaimsPrincipal
is created, filterContext.HttpContext.User is set to User1, and because this.Users
IsNullOrEmpty, this.Users
is set to User1.Identity.Name. Therefore base.AuthorizeCore
will return true. User 1 gets in.
Request 2 comes in from User 2, User2 ClaimsPrincipal
is created, filterContext.HttpContext.User is set to User2, and because this.Users
is not NullOrEmpty (still has the value from User 1)!, this.Users
remains User1.Identity.Name. Therefore base.AuthorizeCore
will return false (User 1 <> User 2). User 2 cannot get in!
Questions:
Why does this.Users
still have a value from a previous Request? Is that by-design? Why?
I thought this.Users
might be empty for each new request? How does this.Users
get cleared?
Maybe I should not check for IsNullOrEmpty, just set this.Users
to "User1,User2", which would allow both Users... But if I get 100's of unique users...
Or maybe I shouldn't be setting this.Users
at all; after all this.Users
seems like it's about authorizing only specific users; I don't need only specific users, I need any authenticated user to get in ; maybe I should leave this.Users
empty!