开发者

Custom RoleProvider with MVC 2.0

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"开发者_StackOverflow中文版 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?


Couple things to consider, first the Membership and Role providers are static and are generally only initialized the first time they are accessed within an application. When setup properly in Web.Config, they will call initialize when they are first accessed.

Through some testing with a basic set of Providers and the default ASP.NET MVC 2 application, I found that the RoleProvider is generally initialized immediately upon application startup. One thing to note though, if you want to have the providers reinitialized, you'll have to stop your development web server (or iis express, or clear the app pool). That way the application is restarted. If you don't, and the providers have already been initialized, it will never hit those break points as it's already been run.

Also, when obtaining an instance of your membership provider, don't do so through creating a new instance of the class (ie var provider = new MyMembershipProvider()), as this will not call the initialize method. Be sure to obtain your provider via Membership.Provider, which will handle all of the setup for your properly configured web.config provider.

A good look at the default MVC 2 application, within the AccountModels file (~line 101) provides a fine example.

_provider = provider ?? Membership.Provider;

The first time it's accessed, it will instantiate the default membership provider and call initialize. After that, it remains static to the application and isn't called again until the application is unloaded from memory. ie. Stop the vs web development server.

Also, another similiar method of obtaining a specific membership provider can also be found at: How do I call Initialize on a custom MembershipProvider?

Hope this helps :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜