views:

439

answers:

1

I have a custom Membership and Role Provider that I am setting up within an application. In ASP .Net Webforms I utilize the same providers and when the app is compiled and run the providers are initialized because of the references in the web.config.

When I move this to MVC and put break points in my "Initialize" methods for these classes the breaks are not hit.

An oddity: If I utilize the "[Authorize(Roles = "MYROLE")]" process within a controller and hit that Action it then goes out to the Roleprovider and calls the "GetRolesForUser" automatically but still never hits the initialize. This is an issue since I need to have certain varibles set up prior to calling any methods.

I know I can call "Initialize" directly but would have thought this would have been done automatically as it was in ASP Webforms.

Do I have to manually initialize these in MVC or am I missing something?

A: 

Jay, I suppose that they work the same way that role/membership providers works in ASP.NET WebForms.

One thing that you should try is to create custom authorize filters that will call your application methods, like this:

public class MyAuthorizeAttribute: FilterAttribute, IAuthorizationFilter
{    
    public string Role { get; set; }

    #region IAuthorizationFilter Members    
     public void OnAuthorization(AuthorizationContext filterContext)    
     {        
      // add your logic here like 
      // var userRoles = MyCustomProvider.GetRolesForUser(filterContext.HttpContext.User.Identity);
      // if(!userRoles.Contains(Role))
      // .....
     }    
    #endregion
}

and then use [MyAuthorize(Role = "MYROLE")] in your actions.

Oleiro