0

I am trying to set up my own CustomRoleProvider to allow users to login to an "Admin Dashboard' for my portal.

My web config file has been updated

 <roleManager enabled="true" defaultProvider="CustomRoleProvider">
  <providers>
    <clear />
    <add name="CustomRoleProvider" type="Namespace.Models.CustomRoleProvider" connectionStringName="DBName" />
  </providers>
</roleManager>

And my CustomRoleProvider set up with GetRolesForUser and IsUserInRole

    public override string[] GetRolesForUser(string username)
    {
        using (var userContext = new MyEntities())
        {
            var user = userContext.AdminTables.SingleOrDefault(u => u.UserName == username);
            var userRoles = userContext.AdminTables.Select(r => r.UserRole);

            if (user == null)
                return new string[] { };
            return user.UserName == null ? new string[] { } :
                userRoles.ToArray();
        }
    }


    public override bool IsUserInRole(string username, string roleName)
    {
        using (var userContext = new MyEntities())
        {
            var user = userContext.AdminTables.SingleOrDefault(u => u.UserName == username);
            var userRoles = userContext.AdminTables.Select(r => r.UserRole);

            if (user == null)
                return false;
            return user.UserRole != null &&
                userRoles.Any(r => r == roleName);
        }
    }

I think the issue is I only have one AdminTable with Username (and Password). If the username is in the AdminTable then they are an Admin.

This is the first time I am trying to Implement CustomerRolePRovider so not sure if I need both GetRolesforUser and IsUserInRole, if there is just one role. Still, I cannot figure out why if the user is listed in the AdminTable I am getting the error above. Help me, please.

The fields in my AdminTable are basically UserName, and UserRole (which is always Admin (for now)).

  • Are you running from VS. When running from VS you do not have Admin unless you start VS by right click shortcut and select Run As Admin. – jdweng May 25 '21 at 13:05
  • Yes, I am running VS as Admin. Would that make a difference though? In my database I have a user "SuperAdmin" who is assigned the Admin role. User can login successfully buy does not get a chance to see the Admin dashboard. – Rayman Bacchus May 25 '21 at 14:51
  • Are you using SQL Server Management Studio? When the login window is set for Windows Credentials the user login account is used. Also when you are on a remote machine the machines have to be in a group (Group Policy) so the user has the same account on local and remote machines. – jdweng May 25 '21 at 14:57
  • I have updated my question - maybe it was not clear enough. It's not happening when I log in to VS or SSMS - they all seem to work fine. This is the site I am trying to build and allow onyl users in my Admin table of the database into the Admin Dashboard. I am basically trying to implement my own CustomRoleProvider based on instructions here https://stackoverflow.com/questions/41940905/asp-net-mvc-how-to-create-a-custom-role-provider – Rayman Bacchus May 25 '21 at 16:05
  • Does `userContext.AdminTables.Select(r => r.UserRole);` return the role? What does your debugger tell you? – mxmissile May 25 '21 at 16:16

0 Answers0