웹 앱 (역할, 사용자)에 대해`aspnetdb` 데이터베이스를 초기화하는 데 권장되는 방법은 무엇입니까?

pepr

웹 응용 프로그램에 대한 인증 데이터베이스를 만들고 채우는 콘솔 응용 프로그램을 작성하려고합니다. 배포 루틴의 일부 여야합니다. 다음 코드는 부분적으로 작동합니다.

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;(정확한 영어 표현이 아닌 번역) ...

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

드미트로 셰브첸코

PreApplicationStartMethodAttributeASP.NET의 일부이며 ASP.NET에서만 호출됩니다. 콘솔 응용 프로그램에서는 아무 작업도 수행하지 않습니다.

역할을 활성화하는 또 다른 방법 이 있습니다 . 이 튜토리얼을 참조하십시오 .

기본적으로 App.config에 추가해야하는 구성은 다음과 같습니다.

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

물론 올바른 데이터베이스를 가리 키도록 역할 관리자를 적절하게 구성해야합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관