views:

91

answers:

1

Is there built in methods into .net framework to do the following:

  1. Get role GUID from user name/user GUID
  2. Get role name from role GUID

So far I have been sending queries to the asp_roles and asp_users tables to obtain that information and I'm wondering if there is a nicer way of doing this?

I have the following methods that I store in the ASPUtilities class:

getRoleGUID(guid userGuid) { LINQ joins }

getRoleGuid(string userName) { LINQ joins  }

getRoleName(guid roleGuid) { LINQ joins  }

EDIT:

I have just looked into extending SQLMembershipProvider examples.

Few examples completely override the SQLMembershipProvider, but I think what I'm interested is just adding few extra methods to deal with the roles by using LINQ. Is this feasible?

+1  A: 

If you extend the SQLMembership provider, it will do the exact same thing, send queries to the database to get that information since the roleID is not stored in session. If you don't want to go through that much trouble, you could do a few things.

  1. Create a custom class with these methods that you can call and will return the ID for you of your role. Essentially, move all of your queries to one location.
  2. Store the role ID in session on login. Call your method you created above once and don't requery everytime. Not 100% of the security risks with this one, however, since you are storing some possibly sensitive role information, the id, in session. Might just be overly cautious though :)
Tommy
Hey,Thanks for the advise. Yes that's what I'm currently doing. I'm not too keen on storing the role in the session variable since it will be a list of roles and user can be added/removed to/from the roles by an admin user, so the information in the session will be out of synch. Utility class seems like a reasonable option, I'm just not too keen on getting some data from .Net classes and some from my own classes, but can't seem to find an alternative so far. Maybe it's not such a bad implementation after all...
vikp
For simplicity and masking the getting data from some .NET classes and your own classes, wrap the .NET methods with your own method name in your utiltiy class. IE, utilityClass.getRoleName() returns httpcontext.current.user.getRoles (or whatever that call is). Might help clean some of that up, but that is really the only benefit to doing this.
Tommy
Hey Tommy, thanks for the idea. It will be "centralised" and there won't be soo much code in the controller/web methods.
vikp