_AppStart.cshtml, PackageManager, WebMatrix

I would try adding SimpleMembersihp to the MVC4 website. Not this way. Template code (like C #) is well suited to support it, but web.config is mostly agnostic - devoid of elements that will configure any particular security mechanism. I followed Scott Allen's MVC4 Pluralsight guide - it's a mixed aspnet membership with EF. Maybe I missed something, but C # membership classes did not interact with aspnet-Membership, they did SimpleMembership. So, I deleted aspnet-Membership, but now I can not get the PackageManager for "update-database". He complains: "You must call the WebSecurity.InitializeDatabaseConnection ... method." So, I added the _AppStart.cshtml file with the call, but PM does not seem to know. _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);
        //}
}

The role "Admin" is not created.?

+5
source share
2 answers

Try to add code

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

To your Application_Start in global.asax.cs

_appStart.cshtml not used in MVC.

+11
source

Possible uses of _AppStart.cshtml In MVC4 Create a new file and place it in the root folder

@{
 // 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

0
source

All Articles