_AppStart.cshtml,PackageManager,WebMatrix

时间:2012-09-23 14:17:16

标签: asp.net-mvc-4

我将SimpleMembersihp添加到MVC4网站是一件简单的事情。不是这样。模板化代码(例如C#)非常适合支持它,但web.config主要是不可知的 - 没有配置任何特定安全机制的元素。我正在关注关于Pluralsight的Scott Allen的MVC4教程 - 将aspnet-Membership与EF混合。也许我错过了一些东西,但C#membership - 类没有与aspnet-Membership交互,他们正在做SimpleMembership。所以,我删除了aspnet-Membership,但现在我无法将PackageManager送到'update-database'。它抱怨“您必须调用WebSecurity.InitializeDatabaseConnection方法......”。所以,我在调用中添加了一个_AppStart.cshtml文件,但PM似乎没有意识到。 _AppStart.cshtml:

@using WebMatrix.WebData;
@{
    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
}

的Web.config:

  <appSettings>
    <add key="enableSimpleMembership" value="true" />
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

   <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

Configuration.cs:

protected override void Seed(eManager.Web.Infrastructure.DepartmentDb context)
{
    context.Departments.AddOrUpdate(d => d.Name,
      new Department() { Name = "Engineering" },
      new Department() { Name = "Sales" },
      new Department() { Name = "Shipping" },
      new Department() { Name = "Human Resources" }
      );

    SimpleRoleProvider roles = new WebMatrix.WebData.SimpleRoleProvider();
    SimpleMembershipProvider membership = new SimpleMembershipProvider();
    if (!roles.RoleExists("Admin"))
    {
        roles.CreateRole("Admin");
    }

    //if (membership.GetUser("ej", false) == null)
    //{
        //    membership.CreateUserAndAccount("ej", "FluffyBunny@1", false);
        //    string[] u = { "ej" };
        //    string[] r = { "Admin" };
        //    roles.AddUsersToRoles(u, r);
        //}
}

未创建“管理员”角色。?

2 个答案:

答案 0 :(得分:11)

尝试添加代码

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);

global.asax.cs

中的Application_Start MVC中未使用

_appStart.cshtml

答案 1 :(得分:0)

可能使用_AppStart.cshtml在MVC4中 通过创建新文件并将其放在根文件夹

@{
 // Initialize WebMail helper
 WebMail.SmtpServer = "your-SMTP-host";
 WebMail.SmtpPort = 25;
 WebMail.UserName = "your-user-name-here";
 WebMail.Password = "your-account-password";
 WebMail.From = "your-email-address-here";
 }

http://www.asp.net/web-pages/overview/ui,-layouts,-and-themes/18-customizing-site-wide-behavior#configuring_email_settings