Reputation: 12211
Ok, so I setup SimpleMembership, and I was under the impression that I had to run WebSecurity.InitializeDatabaseConnection(...)
only once in order to have it set up the database. Later, when I attempt to look up a user id, I get an error telling me that I need to initialize the database before I ever call WebSecurity. Ok... so I put the initialization call back into _ViewStart.cshtml as it suggested. Now I run again and I get the error The "WebSecurity.InitializeDatabaseConnection" method can be called only once.
So I'm damned if I do damned if I don't.
Can someone explain this to me?
Upvotes: 0
Views: 135
Reputation: 2607
You need to place the WebSecurity.InitializeDatabaseConnection(...)
in the DatabaseInit method of the DatabaseConfig App_Start\DatabaseConfig.cs
class like below:
public static void DatabaseInit(string ConnectionName = "DefaultConnection", string userTable = "UserProfile", string userIdColumn = "UserId", string userNameColumn = "UserName", bool autoCreateTable = true)
{
WebSecurity.InitializeDatabaseConnection(ConnectionName, userTable, userIdColumn, userNameColumn, autoCreateTables: autoCreateTable);
}
Hope this helps :)
Upvotes: 1
Reputation: 93444
Yes, it's actually quite simple. You are using some code that requires simple membership PRIOR to where you are normally doing your initialization. So, you get the error message and you add a call to do the initialization, so now your new code works.. but then it gets to the old code which.. initializes the database again and it throws the error.
The problem is basically that you are trying to access the data too early in the pipeline. Initialization must occur once each time the App Domain is created (ie, when the app is first started, or after the app is restarted from being shut down by IIS for inactivity).
This is why you should wrap it in a call to WebSecurity.Initialized
Upvotes: 1