You should call "WebSecurity.InitializeDatabaseConnection" ... But I already have

I read a few SO questions on this topic, but everyone seems to be dealing with where you should put this call.

My problem is different: I already made a call to WebSecurity.InitializeDatabaseConnection() and set breakpoints so that I know that it is executed. But I still get the invalid operation exception message, which I should call.

Unlike most of the other issues that complement it in MVC controller action, I come across this HttpModule , which I wrote to perform authentication for the WebAPI REST controller. The Init call contains the WebSecurity.InitializeDatabaseConnection call. The OnAuthenticationRequest method OnAuthenticationRequest extracts the username and password from the request authorization header and calls the ValidateUser SimpleMembershipProvider method. This is where I get the exception

You must call the WebSecurity.InitializeDatabaseConnection method before calling any other method of the WebSecurity class.

So,

a) why I get this exception when I have already fulfilled the conditions so as not to get it.
b) what can be done about this?

+4
source share
2 answers

After you came across the same problem and set many breakpoints, I found a solution for my environment, hope others can benefit from this:

inside the App_Start folder

 public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { var attr=new InitializeSimpleMembershipAttribute(); // here is the important part attr.OnActionExecuting(new ActionExecutingContext()); filters.Add(attr); } } 

Reference Information. In my application, I have 2 views:

  • / Account / Registration: this is part of the standard asp.mvc 4 templates and has the attribute [AllowAnonymous]
  • / Task: this is an "Index" -Note of a todo list, TaskController has the attribute [Authorize (Roles = "Administrator")]

I always had a problem in the following call:

  • debugging session in browser
  • successful login
  • open the "/ Task" view requiring authorization
  • Switch to visual studio and leave the browser open.
  • stop debugging
  • change the code inside the controller (just set some spaces)
  • start debugging session
  • click update button in browser

At this point, the "OnActionExecuting" method inside the InitializeSimpleMembershipAttribute class has not yet been called (determined by setting breakpoints)!

But when I opened the View, which does not require authorization (i.e. / Account / Register), the "OnActionExecuting" method was called. After that, I could call / Ask without errors.

Thus, the solution was to create a “pseudo-course” for “OnActionExecuting” at application startup.

+1
source

You do not need to start a database connection every time you call HttpModule. Just do it once when you start the application. Here's an article that explains how to use SimpleMembership to authenticate / authorize web API calls . There is also a link to the source code for a sample project.

0
source

All Articles