Skip to content

Commit

Permalink
Merge pull request #1 from aspnet/ControllerUpdates
Browse files Browse the repository at this point in the history
Everything but the kitchen async... (Updates to how Music Store controllers use data)
    
Specifically:
    - Dispose contexts
    - Use async wherever possible
    - Stop using initializers (currently hard-coded to drop and recreate each run)
    - Some general cleanup
    - Stop using AttachDbFilename
    
Not included here:
    - No major changes to app structure
    - No major changes to data model
    - No major changes to error handling, concurrency, etc.
  • Loading branch information
ajcvickers committed Jan 29, 2014
2 parents 53268f3 + c20584b commit b8f5824
Show file tree
Hide file tree
Showing 15 changed files with 4,143 additions and 958 deletions.
4 changes: 0 additions & 4 deletions .nuget/packages.config

This file was deleted.

2 changes: 2 additions & 0 deletions MusicStore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicStore.k10", "src\Music
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicStore.net45", "src\MusicStore\MusicStore.net45.csproj", "{9C8A2D1F-D430-46DF-8F00-39E378EC8FB2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{44621553-AA7D-4893-8834-79582A7D8348}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
60 changes: 34 additions & 26 deletions src/MvcMusicStore/App_Start/Startup.App.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
using Microsoft.AspNet.Identity;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using MvcMusicStore.Models;
using Owin;
using System.Configuration;
using System.Threading.Tasks;

namespace MvcMusicStore
{
public partial class Startup
{
private const string RoleName = "Administrator";

public void ConfigureApp(IAppBuilder app)
{
System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());
using (var context = new MusicStoreEntities())
{
context.Database.Delete();
context.Database.Create();

new SampleData().Seed(context);
}

CreateAdminUser();
CreateAdminUser().Wait();
}

private async void CreateAdminUser()
private async Task CreateAdminUser()
{
string _username = ConfigurationManager.AppSettings["DefaultAdminUsername"];
string _password = ConfigurationManager.AppSettings["DefaultAdminPassword"];
string _role = "Administrator";

var context = new ApplicationDbContext();
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var username = ConfigurationManager.AppSettings["DefaultAdminUsername"];
var password = ConfigurationManager.AppSettings["DefaultAdminPassword"];

var role = new IdentityRole(_role);
var result = await roleManager.RoleExistsAsync(_role);
if (result == false)
using (var context = new ApplicationDbContext())
{
await roleManager.CreateAsync(role);
}
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

var user = await userManager.FindByNameAsync(_username);
if (user == null)
{
user = new ApplicationUser { UserName = _username };
await userManager.CreateAsync(user, _password);
await userManager.AddToRoleAsync(user.Id, _role);
var role = new IdentityRole(RoleName);

var result = await roleManager.RoleExistsAsync(RoleName);
if (!result)
{
await roleManager.CreateAsync(role);
}

var user = await userManager.FindByNameAsync(username);
if (user == null)
{
user = new ApplicationUser { UserName = username };
await userManager.CreateAsync(user, password);
await userManager.AddToRoleAsync(user.Id, RoleName);
}
}
}
}
Expand Down
Loading

0 comments on commit b8f5824

Please sign in to comment.