English 中文(简体)
ASP.NET Provider with Different Types of Roles
原标题:

I have different types of Roles to take into account when authorizing a user. For example: John must have Position Manager and be part of the Office Supplies Department to order a new computer.

Problem with Roles is Roles.GetRolesForUser("John") can only return a string array.

Should I go with a custom roleProvider and custom roleManager? or should I develop a custom ProfileManager to add methods like GetUsersWithProfileProperties()?

Any suggestion is welcome!

Thibaut

EDIT: the above example is simplified I could have a much as 4 types of roles which are 4 different collections.

EDIT: I found a very similar question

最佳回答

why not create a "CompositeRoleProvider" with a Path-To-Level typew convention for accessing each subordinate role provider. You will still have to create multiple role providers, but your Composite or Top-Level Provider does all of the work for you. I plan to do a similar thing with ProfileProvider

问题回答

From what you write; I believe that everything you need is currently available out of the box:

    // Return all Users in a  Role
    string[] users;
    users = Roles.GetUsersInRole("RoleName");
    // Return all Roles for a User
    string[] roles;
    roles = Roles.GetRolesForUser();
    // Search through Membership store locating users with a role
    MembershipUserCollection mu;
    mu = Membership.GetAllUsers();
    // Loop through all membership users looking for users in a role

    foreach(MembershipUser m in mu){
        if(Roles.IsUserInRole(m.UserName, "Role Name")){
            // Do something

            // We can even nest to x levels
            if (Roles.IsUserInRole(m.UserName, "Another Role")){

                // Do something else
            }
        }
    }

Please clarify if I have misunderstood your question.

I m studying how to solve a pretty similar problem and I ve come to a conclusion that the best thing to do is to implement a custom role provider.

I m using this (http://msdn.microsoft.com/en-us/library/317sza4k(v=vs.100).aspx) as a base and I will implement my methods like (IsManager, GetDepartment, ecc).

Data will be stored in custom tables that are joined to the aspnet_users table.

Hope it may help someone in the future :)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签