简体   繁体   中英

The recommended way for initializing the `aspnetdb` database for the web app (roles, users)?

I am trying to write a console application that would create and populate the authentication database for the web application. It should be the part of the deployment routine. The following code partly works:

using System.Web.Management;
using System.Web.Security;

namespace InitAspnetdbAppRolesAndUsers
{
    class Program
    {
        static string SQLServerName = @"COMPUTER\SQLINSTANCE";
        static string ASPNETdbName = "aspnetdb";

        static void Main(string[] args)
        {
            // Create the aspnetdb database.
            SqlServices.Install(SQLServerName, ASPNETdbName, SqlFeatures.All);

            // Set the application.
            Membership.ApplicationName = "my_app";

            const string adminRoleName = "Administrator";
            const string adminUserName = "admin";
            const string adminPassword = "adminPa$$word1234";

            Roles.Enabled = true;

            if (!Roles.RoleExists(adminRoleName))
                Roles.CreateRole(adminRoleName);

            if (Membership.GetUser(adminUserName) == null)
                Membership.CreateUser(adminUserName, adminPassword);

            if (!Roles.IsUserInRole(adminUserName, adminRoleName))
                Roles.AddUserToRole(adminUserName, adminRoleName);
        }
    }
}

The SqlServices.Install(SQLServerName, ASPNETdbName, SqlFeatures.All); is executed and the aspnetdb empty database is created without problems. However, the Roles.Enabled = true; crashes with (not exact English wording, translated)...

Unhandled exception: System.InvalidOperationException: The method can be called
only during initialization phase of the application, before it is launched.
Declare the method that will be called in the phase using the attribute
PreApplicationStartMethodAttribute.
   in System.Web.Compilation.BuildManager.ThrowIfPreAppStartNotRunning()
   in System.Web.Security.Roles.set_Enabled(Boolean value)
   in InitAspnetdbAppRolesAndUsers.Program.Main(String[] args) 
in D:\ASP_NET_snippets\InitAspnetdbAppRolesAndUsers\Program.cs:line 23

Can the PreApplicationStartMethodAttribute be added also for a console application ? If yes, how can this be done? Is there any other way to populate the asbnetdb database? The goal is to set the names, other properties, etc. that were extracted from some other database

PreApplicationStartMethodAttribute is a part of ASP.NET and will only be called by ASP.NET. It will not do anything in a console application.

There is another way to enable roles, see this tutorial .

Basically, the configuration you need to add to your App.config is this:

<configuration>
    <system.web>
        <roleManager enabled="true" />
    </system.web>
</configuration>

Of course, you also need to properly configure your role manager to point it to the right database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM