简体   繁体   中英

Manage Roleprovider for two or more tables in asp.net mvc

I'm making a website using asp.net mvc4 & EF6 where I've two tables for admins & users and I want to set roles for Authorization for each of them. So far I've successfully managed to set role for users but I can't figure how to set role in my custom RoleProvider class if I've two or more models. Here are my codes,

public class MgtRoleProvider : RoleProvider
{
    public override string[] GetRolesForUser(string username)
    {
        rental_dbEntities db = new rental_dbEntities();
        string userRole = db.TblUsers.Where(a => a.username == username).FirstOrDefault().role;
        string[] result = { userRole };
        return result;
    }
}

How can I set two or more models and return their role values in GetRolesForUser method? Need this help badly. Thanks.

Add in a second query for your TblAdmins table and return them both:

public class MgtRoleProvider : RoleProvider
{
    public override string[] GetRolesForUser(string username)
    {
        rental_dbEntities db = new rental_dbEntities();
        string userRole = string.Empty;
        string adminRole = string.Empty;

        var user = db.TblUsers.Where(a => a.username == username).FirstOrDefault()

        if (user != null)
        {
            userRole = user.role;
        }

        var admin = db.TblAdmins.Where(a => a.username == username).FirstOrDefault();       

        if (admin != null)
        {
             adminRole = admin.role;
        }

        string[] result = { userRole, adminRole };
        return result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM