A Custom Membership Provider *Without* a Database?

落花浮王杯 提交于 2019-12-20 06:07:11

问题


I've been looking around various SO questions, blog posts, and so forth regarding the changes to membership providers in MVC 4. While I like many of the changes and the simplification of things (particularly the out-of-the-box external login support), I'm as yet unable to find one seemingly-simple thing...

How can I override the membership/role provider with a custom one that uses another data source?

I don't want to replace WebSecurity or anything that drastic, since it handles all of the things I want it to handle. (Cookie management, for example.) I don't want to do anything that will break the existing support for external login providers, as my application uses those in conjunction with local logins. All I'm looking to do is interject between the membership system and the database behind it and customize that data fetching.

The tables that it creates by default are:

  • UserProfile
  • webpages_Membership
  • webpages_OAuthMembership
  • webpages_Roles
  • webpages_UsersInRoles

And while that works, I have cause to abstract this behind some custom objects and break the coupling between the application and the database. (Basically, I have some custom domain models full of business logic which should handle all of this, and I want the membership/role system to use those models instead of the database. Those models might get their persisted data from a database, they might not. That shouldn't matter.)

Most of the references I find for customization claim that it's easy to do, all I have to do is modify the UserProfile model, include my new custom fields in various calls, and update the call to InitializeDatabaseConnection to point it to my database schema.

But I'm not using a database. (Or, more specifically, as far as the application layer is concerned there's no such thing as a database. Only my domain models.) So that easy customization doesn't really change anything.

Is there a way to interject between the membership system and the database so I can make a custom data provider for the existing membership provider?


回答1:


You should be able to create a custom SimpleRoleProvider and plug it into your application by modifying the web.config to something like this:

<add name="SimpleRoleProvider" type="MyCustomRoleProvider.SimpleRoleProvider, MyCustomRoleProvider"/>

Where MyCustomRoleProvider is the name of the assembly that contains your implementation of SimpleRoleProvider.

Then just rip out the initialization for SimpleMembership. This article describes how to simplify the initialization process for SimpleMembership. Basically remove the InitializeSimpleMembership attribute from the AccountController and move the initialization to the Global.asax Application_Start method. But in your case you would want to add any initialization required for your custom SimpleRoleProvider, if there is any required.

For the implementation your are describing you will most likely have to also create a custom SimpleMembershipProvider.



来源:https://stackoverflow.com/questions/16843880/a-custom-membership-provider-without-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!