0

I am implementing my custom Role provider. I need access to some additional data in the following functions and I am not sure if it is possible. Is there a way to instantiate the CustomRoleProvider class by code and set some properties to pass the additional data? If it is not possible, what other alternatives are there to achieve the custom role management? Thanks for any input.

public class CustomRoleProvider : RoleProvider
{
    public override string[] GetRolesForUser(string username)
    { }

    public override bool IsUserInRole(string username, string roleName)
    { }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Jyina
  • 2,530
  • 9
  • 42
  • 80

1 Answers1

1

You can use dependency injection to inject a configuration dependency. Something like:

public class CustomRoleProvider : RoleProvider
{
   [Dependency]    
   public IRoleProviderConfig Config { get; set; }

   public override string[] GetRolesForUser(string username)
   { 
       int value = Config.GetXyz();
   }

   public override bool IsUserInRole(string username, string roleName)
  { }
}

You can learn more about dependency injection here: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/dependency-injection-in-mvc-3-was-made-easier.aspx

Faisal Mansoor
  • 2,041
  • 1
  • 21
  • 25