Sunday, December 23, 2012

What happens if you need to test a user role where it isn't set up yet?

In my AccountController's LogIn method, since I've added Role based authentication to the site,
I wanted to use User.IsInRole( "Admin" ) to check for an Admin role.

Of course that doesn't work!

So instead, I had to do this:

decimal id = Identity.LookupId( details.MemberName, db );
Identity Id = Identity.Load( id, db );
if( Id.RoleIndex == 1 )


That's two hits to the database, but I do those two hits on almost every page so I know that the server can easily handle the task at hand.

Why was I interested in this:

Well we have two different controllers and I wanted the Admin role to be sent to the AdminController and normal Users to be sent to the BackOfficeContoller.

No comments:

Post a Comment