初始化了`aspnetdb`数据库的Web应用程序(角色,用户)推荐的方法? [英] The recommended way for initializing the `aspnetdb` database for the web app (roles, users)?

查看:187
本文介绍了初始化了`aspnetdb`数据库的Web应用程序(角色,用户)推荐的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写这将创建并填充为Web应用程序的验证数据库中的控制台应用程序。它应该是在部署程序的一部分。下面code部分作品:

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);
        }
    }
}

SqlServices.Install(SQLSERVERNAME,ASPNETdbName,SqlFeatures.All); 执行与 ASPNETDB 空数据库没有问题的创建。但是, Roles.Enabled = TRUE; 与崩溃(不准确的英语措辞,翻译)...

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

可以在 preApplicationStartMethodAttribute 来还增加了一个控制台应用程序?如果是,如何才能做到这一点?是否有任何其他的方式来填充 asbnetdb 数据库?我们的目标是设定从某些其他数据库中提取的名称,其它性质等

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

推荐答案

<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.$p$papplicationstartmethodattribute(v=vs.110).aspx\"相对=nofollow> preApplicationStartMethodAttribute 是ASP.NET的一部分,将仅由ASP.NET调用。它不会在控制台应用程序做任何事情。

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.

基本上,你需要添加到您的App.config配置是这样的:

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.

这篇关于初始化了`aspnetdb`数据库的Web应用程序(角色,用户)推荐的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆