Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 05, 2012 05:59 PM by shapper
Contributor
3932 Points
3789 Posts
Feb 05, 2012 03:35 PM|LINK
Hello,
Is there a way to get all Roles fo the current user? I tried the following:
((RolePrincipal)HttpContext.Current.User).GetRoles()
But I get the following error:
Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'System.Web.Security.RolePrincipal'.
Any idea how to get the user roles?
Thank You,
Miguel
Member
309 Points
285 Posts
Feb 05, 2012 03:50 PM|LINK
Using System.Web.security;
Roles.GetRolesForUser
Feb 05, 2012 03:55 PM|LINK
I am not using the Membershipt/Roles provider.
I am using only Forms Authentication and a custom Membership/Roles system.
So I am looking to get the Roles of the current user but from the context data.
Any idea?
Feb 05, 2012 03:57 PM|LINK
By the way, in forms authentication the roles are usually added as follows:
HttpContext.Current.User = new GenericPrincipal(id, roles);
So this is what I am looking for ...
No clue without knowing what your custom Role provider is and how it stores the data
Feb 05, 2012 04:13 PM|LINK
For example, when I use the following:
HttpContext.Current.User.IsInRole("role")
I am not using the role provider.
So balically I am looking to get all the roles to which the method IsInRole is comparing to ...
All-Star
112752 Points
18373 Posts
Moderator
Feb 05, 2012 04:26 PM|LINK
Feb 05, 2012 05:59 PM|LINK
MetalAsp.Net Do you mean you're using a custom membership/role PROVIDER? Or something of your own creation?
Something of my own creation (Not provider) on top of Forms Authentication.
So I am using the following:
public void OnAuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value); String[] roles = ticket.UserData.Split(','); FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; HttpContext.Current.User = new GenericPrincipal(id, roles); } } } } // OnAuthenticateRequest
So basically I am looking to get the GenericPrincipal roles from the HttpContext.Current.User.
shapper
Contributor
3932 Points
3789 Posts
Get User Roles
Feb 05, 2012 03:35 PM|LINK
Hello,
Is there a way to get all Roles fo the current user? I tried the following:
But I get the following error:
Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'System.Web.Security.RolePrincipal'.
Any idea how to get the user roles?
Thank You,
Miguel
RDO
Member
309 Points
285 Posts
Re: Get User Roles
Feb 05, 2012 03:50 PM|LINK
Using System.Web.security;
Roles.GetRolesForUser
Christian Forum
shapper
Contributor
3932 Points
3789 Posts
Re: Get User Roles
Feb 05, 2012 03:55 PM|LINK
I am not using the Membershipt/Roles provider.
I am using only Forms Authentication and a custom Membership/Roles system.
So I am looking to get the Roles of the current user but from the context data.
Any idea?
shapper
Contributor
3932 Points
3789 Posts
Re: Get User Roles
Feb 05, 2012 03:57 PM|LINK
By the way, in forms authentication the roles are usually added as follows:
HttpContext.Current.User = new GenericPrincipal(id, roles);
So this is what I am looking for ...
RDO
Member
309 Points
285 Posts
Re: Get User Roles
Feb 05, 2012 03:57 PM|LINK
No clue without knowing what your custom Role provider is and how it stores the data
Christian Forum
shapper
Contributor
3932 Points
3789 Posts
Re: Get User Roles
Feb 05, 2012 04:13 PM|LINK
For example, when I use the following:
HttpContext.Current.User.IsInRole("role")
I am not using the role provider.
So balically I am looking to get all the roles to which the method IsInRole is comparing to ...
MetalAsp.Net
All-Star
112752 Points
18373 Posts
Moderator
Re: Get User Roles
Feb 05, 2012 04:26 PM|LINK
shapper
Contributor
3932 Points
3789 Posts
Re: Get User Roles
Feb 05, 2012 05:59 PM|LINK
Something of my own creation (Not provider) on top of Forms Authentication.
So I am using the following:
public void OnAuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value); String[] roles = ticket.UserData.Split(','); FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; HttpContext.Current.User = new GenericPrincipal(id, roles); } } } } // OnAuthenticateRequestSo basically I am looking to get the GenericPrincipal roles from the HttpContext.Current.User.