2016-12-14 12:15:39 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
|
|
|
|
|
{
|
|
|
|
|
using AspNetCore.Identity;
|
|
|
|
|
using EntityFrameworkCore;
|
|
|
|
|
using Extensions.Logging;
|
2017-01-30 11:35:16 +01:00
|
|
|
|
using global::Identity.API.Data;
|
|
|
|
|
using global::Identity.API.Models;
|
2016-12-14 12:15:39 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
public class ApplicationContextSeed
|
|
|
|
|
{
|
|
|
|
|
private readonly IPasswordHasher<ApplicationUser> _passwordHasher;
|
|
|
|
|
|
|
|
|
|
public ApplicationContextSeed(IPasswordHasher<ApplicationUser> passwordHasher)
|
|
|
|
|
{
|
|
|
|
|
_passwordHasher = passwordHasher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
|
|
|
|
|
{
|
|
|
|
|
int retryForAvaiability = retry.Value;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var context = (ApplicationDbContext)applicationBuilder
|
|
|
|
|
.ApplicationServices.GetService(typeof(ApplicationDbContext));
|
|
|
|
|
|
|
|
|
|
context.Database.Migrate();
|
|
|
|
|
|
|
|
|
|
if (!context.Users.Any())
|
|
|
|
|
{
|
|
|
|
|
context.Users.AddRange(
|
|
|
|
|
GetDefaultUser());
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (retryForAvaiability < 10)
|
|
|
|
|
{
|
|
|
|
|
retryForAvaiability++;
|
|
|
|
|
var log = loggerFactory.CreateLogger("catalog seed");
|
|
|
|
|
log.LogError(ex.Message);
|
|
|
|
|
await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ApplicationUser GetDefaultUser()
|
|
|
|
|
{
|
|
|
|
|
var user =
|
|
|
|
|
new ApplicationUser()
|
|
|
|
|
{
|
2016-12-14 18:23:57 +01:00
|
|
|
|
CardHolderName = "DemoUser",
|
|
|
|
|
CardNumber = "4012888888881881",
|
2016-12-14 12:15:39 +01:00
|
|
|
|
CardType = 1,
|
2016-12-14 18:23:57 +01:00
|
|
|
|
City = "Redmond",
|
|
|
|
|
Country = "U.S.",
|
|
|
|
|
Email = "demouser@microsoft.com",
|
2016-12-14 12:15:39 +01:00
|
|
|
|
Expiration = "12/20",
|
|
|
|
|
Id = Guid.NewGuid().ToString(),
|
2016-12-14 18:23:57 +01:00
|
|
|
|
LastName = "DemoLastName",
|
|
|
|
|
Name = "DemoUser",
|
|
|
|
|
PhoneNumber = "1234567890",
|
|
|
|
|
UserName = "demouser@microsoft.com",
|
|
|
|
|
ZipCode = "98052",
|
|
|
|
|
State = "WA",
|
|
|
|
|
Street = "15703 NE 61st Ct",
|
|
|
|
|
SecurityNumber = "535",
|
|
|
|
|
NormalizedEmail = "DEMOUSER@MICROSOFT.COM",
|
|
|
|
|
NormalizedUserName = "DEMOUSER@MICROSOFT.COM",
|
2016-12-14 12:15:39 +01:00
|
|
|
|
SecurityStamp = Guid.NewGuid().ToString("D")
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-14 18:23:57 +01:00
|
|
|
|
user.PasswordHash = _passwordHasher.HashPassword(user, "Pass@word1");
|
2016-12-14 12:15:39 +01:00
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|