Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSecurity.InitializeDatabaseConnection method can be called only once

I'm trying Windows Azure to host an MVC4 web application. I've created a test app, using VS2012 MVC4 internet application template and added a custom Model and Controller to it.

I've published it on Azure and managed to get 'update-database' apply migrations to the Azure Database.

When i try the app locally, but using the Azure SQL database, it works fine. I can login/register and use my test controller.

When i try the app online, i can use the test controller but login or register links give the following exception:

Server Error in '/' Application.

The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: The "WebSecurity.InitializeDatabaseConnection" method can be called only once.]
   WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) +123
   WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +51
   WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +52
   MembershipTest2.Filters.SimpleMembershipInitializer..ctor() +193

Do you have any idea where that comes from ? If i debug (the local version), this method is only called once.

Thanks.

like image 766
malhadeff Avatar asked Oct 25 '12 16:10

malhadeff


2 Answers

You could try encapsulating the call(s) to that method to ensure it's not called more then once

                if (!WebMatrix.WebData.WebSecurity.Initialized)
                {
                    WebSecurity.InitializeDatabaseConnection(...);
                }
like image 136
Gabriel P. Avatar answered Oct 17 '22 05:10

Gabriel P.


in my case I had both

(in web.config)

<add key="enableSimpleMembership" value="true" />

and

(in _ViewStart.cshtml)

WebSecurity.InitializeDatabaseConnection("club", "Account", "UserID", "UserName", autoCreateTables: true);

Solution: it seems you cannot have both, so remove one

like image 21
goran Avatar answered Oct 17 '22 04:10

goran