parapura rajkumar
parapura rajkumar

Reputation: 24403

how to get role from id in asp.net sql membership provider?

MembershipProvider has methods

public abstract MembershipUser GetUser(
    Object providerUserKey,
    bool userIsOnline
)

to look up users by their unique identifier. This is useful if you want to have you own database and associate some objects of yours to certain memberships.

Can you do the same with RoleProvider ? There doesn't seem to be any methods to map roles to their unique identifier or vice versa.

As a side note is it a good idea to just add your own tables to the asp.net sql database or is it better to have a separate database for your own objects and have foreign keys into the asp.net sql membership table.

Upvotes: 2

Views: 4602

Answers (3)

Andomar
Andomar

Reputation: 238086

On a login page, the Page.User variable is still set to anonymous. If you'd like to verify roles before login, you can construct your own GenerpicPrincial like:

var principal = new GenericPrincipal(
    new GenericIdentity(username), 
    Roles.GetRolesForUser(username));

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

There's no such thing as an "asp.net sql database". There's simply the default database that asp.net creates for you if you haven't specified one. There is no reason to use this default database seperately from your regular database. This is why it has a connection string.

The whole point of membership is that it's flexible, to use as you see fit.

Upvotes: 0

Kevin Main
Kevin Main

Reputation: 2344

There is no way within the API to return a role from an ID - you can get all roles via

Roles.GetAllRoles();

Or you can get all roles for a user by

Roles.GetRolesForUser(userName);

But not a single role.

I would guess the reason for this is that roles on there own are probably not that useful (at least as far as Microsoft are concerned).

Of course roles are just a table within the database so you can easily add this functionality yourself with some straight SQL - it will just take a little more work.

In terms of additional data - I normally create my own tables within the same database with foreign keys as you have suggested above. Although if you have not heard of Profile Provider (http://msdn.microsoft.com/en-us/library/0580x1f5.aspx) you may want to look into that as well.

Upvotes: 2

Related Questions