Answered by:
How to call “WebSecurity.InitializeDatabaseConnection” method before “WebSecurity” class

Question
-
User1571524970 posted
Hi guys,
I am trying to seed membership into my asp.net mvc application (in the migrations/config.cs file)
protected override void Seed(Logintest.Models.ApplicationDbContext
context)
{
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)Membership.Provider;
if(!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
if(membership.GetUser("jay", false) == null)
{
membership.CreateUserAndAccount("jay", "otoole");
}
if(!roles.GetRolesForUser("jay").Contains("Admin"))
{
roles.AddUsersToRoles(new[] { "jay" }, new[] { "admin" });
}
}
I also enabled the Role Manager and the Membership Manager in Web.config
<roleManager enabled="true" defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,
WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,
WebMatrix.WebData"/>
</providers>
</membership>
Although when I try to Update-Database in Package Manager Console in order to run the Seed method, I get the following error:You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
I can not find the _AppStart.cshtml file so I created it and placed the following in it:
@using System.Configuration;
@{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
After trying the above I still get the same error. Can someone please point me in the right direction? i.e. how do I call the "WebSecurity.InitializeDatabaseConnection" method before the "WebSecurity" class?
Sunday, July 7, 2019 4:07 PM
Answers
-
User1520731567 posted
Hi darego,
According to your descriptions,I suggest you could add the code below to Global.aspx.cs inside protected void Application_Start() and should appear at the top before any other registrations.
This way, it will always be Initializedbefore an other operations.
private static SimpleMembershipInitializer _initializer; private static object _initializerLock = new object(); private static bool _isInitialized; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } public class SimpleMembershipInitializer { public SimpleMembershipInitializer() { using (var context = new UsersContext()) context.UserProfiles.Find(1); if (!WebSecurity.Initialized)//use the WebSecurity.Initialized to check if it was already called. WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } }
More details,you could refer to:
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 8, 2019 8:28 AM
All replies
-
User1520731567 posted
Hi darego,
According to your descriptions,I suggest you could add the code below to Global.aspx.cs inside protected void Application_Start() and should appear at the top before any other registrations.
This way, it will always be Initializedbefore an other operations.
private static SimpleMembershipInitializer _initializer; private static object _initializerLock = new object(); private static bool _isInitialized; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } public class SimpleMembershipInitializer { public SimpleMembershipInitializer() { using (var context = new UsersContext()) context.UserProfiles.Find(1); if (!WebSecurity.Initialized)//use the WebSecurity.Initialized to check if it was already called. WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } }
More details,you could refer to:
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 8, 2019 8:28 AM -
User1571524970 posted
@Yuki Thanks a lot! Works fine now
Monday, July 8, 2019 10:49 AM