1

Recently I asked a question about how to use ajax calls to authenticate user in asp.net mvc, and I got my answer.

But then is decided to use 'Roles' property of 'GenericPrincipal' class

so I changed my code as follow to contain 'Roles':

HttpContext.User = new System.Security.Principal.
                    GenericPrincipal(new System.Security.Principal.GenericIdentity(login.LoginName),
                     userRole);

In my site.master view, I check to see which kind of roles users belongs to and I show appropriate menu, but when I watch 'HttpContext.Current.User.Identity' values during debugging, I see m_roles=string[0] and 'IsInRole("Admin")' returns false.

How could it be fixed?

Community
  • 1
  • 1
Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80
  • string[] userRole =new string[]{"Admin"} – Babak Fakhriloo Jan 03 '12 at 19:05
  • @persianDev in my webconfig i have because i dont want to get the roles from SQL, thats why Im setting the roles by hand as you did... but exactly right in my controller when i try to check IsInRole("..." i get always false... any clue? – VAAA Jul 24 '12 at 19:00

1 Answers1

0

Try setting the current thread principle:

   System.Threading.Thread.CurrentPrincipal = principal;

as is shown here: http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/5f9735a9-096b-47af-963d-e95130cad6b4

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71