Role management without provider?

In my application, I need to check the roles for the loggind user to determine if the user can see some controls or not.

at first I use the loginview template from MS, but since I have no db users, no db roles, therefore, I could not add a role provider, so I could not use the Role class to check users \ role

As in my case, I have a session with information about the user and his roles, and I need to check these roles to determine which control will be enabled for the user, but with the standard way "using .net built into the class or code "

+3
source share
2 answers

asp.net/ /, , .

, , , System.Web.Security.RoleProvider, , , :

  • FindUsersInRole
  • GetRolesForUser
  • GetUsersInRole
  • IsUserInRole

, :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Security;

public class MyCustomRoleProvider : RoleProvider
{
    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
    }

    public override string[] GetRolesForUser(string username)
    {
    }

    public override string[] GetUsersInRole(string roleName)
    {
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        return GetUsersInRole(roleName).Contains(username);
    }
}

. Visual Studio , GetAllRoles throws new NotImplementedException(), "" , , " ", - , .

roleManager web.config system.web :

<roleManager defaultProvider="NameOfYourRoleProvider" enabled="true">
    <providers>
        <clear />
         <add name="NameOfYourRoleProvider" type="Namespace.To.Your.Class.And.Class.Name, Name.Of.Assembly.Containing.Your.RoleProvider" />
    </providers>
</roleManager>

, RoleProvider asp.net, , HttpContext.Current.Session ( , HttpContext.Current isn ' t null ), using System.Web;.

+7

, , , :

, :

public class MyRoleProvider : System.Web.Security.RoleProvider
{
    public override string[] GetRolesForUser(string username)
    {
          // check a database or an xml file etc.
    }

    public override bool IsUserInRole(string username, string roleName)
    {
         // check a database or an xml file etc.
    }    
}

web.config:

<roleManager enabled="true" defaultProvider="MyRoleProvider">
  <providers>
    <clear />
    <add name="MyRoleProvider" type="MyNameSpace.MyRoleProvider, MyProjectOrAssemblyName" />
  </providers>
</roleManager>

( )

+4

All Articles