InitializeSimpleMembership filter issue

纵然是瞬间 提交于 2019-12-11 19:46:02

问题


I have MVC 4 application with out of the box Simple Membership mechanism implemented. Recently I have discovered that every single time I call any of the controllers in my application OnActionExecuting method in InitializeSimpleMembershipAttribute filter is executed! Even though I have commented out InitializeSimpleMembership attribute on Account controller like so:

[Authorize]
//[InitializeSimpleMembership]
public class AccountController : Controller
{

the filter is still getting called! Where is this call coming from?


回答1:


I would usually delete the InitializeSimpleMembership file which is in the Filters folder by default. You want it to initialize once at start up so the best place for that is Global.asax

Now to keep the structure of the application_start() method consistent in Global.asax, I would add it to a config file being called by that method. You could create a new one but I would usually just add it to AuthConfig. AuthConfig has the default OAuth code, so it makes sense to keep all the authentication methods together.

The Method in called RegisterAuth is called by global.asax once at start up. Just add

//Local Authentication
            WebSecurity.InitializeDatabaseConnection("MyDbConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);

You will have a few errors on build which are resolved by removing using statements referencing the filters folder (maybe only if you have resharper) and removing the InitializeSimpleMembership attribute (it is no longer needed).

In my example I have autoCreateTables to false as I use a Database project in my solution. If you use another method such as code-first you may want that to true




回答2:


You should check the _AppStart.cshtml file if there is any. Or search the code that intialize the membership. It might look like this:

if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("UserConnectionString", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}


来源:https://stackoverflow.com/questions/16666116/initializesimplemembership-filter-issue

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