Seed for identity service (jdoe@eshop.com)
This commit is contained in:
parent
d9d5c6ab7a
commit
7ba1c339c4
@ -39,10 +39,10 @@ services:
|
|||||||
image: eshop/identity
|
image: eshop/identity
|
||||||
environment:
|
environment:
|
||||||
- SpaClient=http://localhost:5104
|
- SpaClient=http://localhost:5104
|
||||||
- ConnectionString=Server=identity.data;Database=aspnet-Microsoft.eShopOnContainers;User Id=sa;Password=Pass@word
|
- ConnectionStrings__DefaultConnection=Server=identity.data;Database=aspnet-Microsoft.eShopOnContainers;User Id=sa;Password=Pass@word
|
||||||
#- MvcClient=http://104.40.62.65:5100 #Remote: VM Needs to have public access at 5105.
|
#- MvcClient=http://104.40.62.65:5100 #Remote: VM Needs to have public access at 5105.
|
||||||
- MvcClient=http://localhost:5100 #Local: You need a entry in windows host file to run identity in local docker.
|
- MvcClient=http://localhost:5100 #Local: You need a entry in windows host file to run identity in local docker.
|
||||||
#10.0.75.1:5105
|
#10.0.75.1:5105 CCE/TODO: try to avoid host entry.
|
||||||
ports:
|
ports:
|
||||||
- "5105:5105"
|
- "5105:5105"
|
||||||
depends_on:
|
depends_on:
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
|
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
|
||||||
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
|
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
|
||||||
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
|
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
|
||||||
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
|
||||||
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
|
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
|
||||||
<NuGetPackageImportStamp>
|
<NuGetPackageImportStamp>
|
||||||
</NuGetPackageImportStamp>
|
</NuGetPackageImportStamp>
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
|
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
|
||||||
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
|
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
|
||||||
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
|
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
|
||||||
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
|
||||||
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
|
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
|
||||||
<NuGetPackageImportStamp>
|
<NuGetPackageImportStamp>
|
||||||
</NuGetPackageImportStamp>
|
</NuGetPackageImportStamp>
|
||||||
|
@ -104,7 +104,6 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
|||||||
};
|
};
|
||||||
|
|
||||||
await _loginService.SignIn(user);
|
await _loginService.SignIn(user);
|
||||||
|
|
||||||
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
|
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
|
||||||
if (_interaction.IsValidReturnUrl(model.ReturnUrl))
|
if (_interaction.IsValidReturnUrl(model.ReturnUrl))
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
|
||||||
|
{
|
||||||
|
using AspNetCore.Identity;
|
||||||
|
using EntityFrameworkCore;
|
||||||
|
using Extensions.Logging;
|
||||||
|
using global::eShopOnContainers.Identity.Data;
|
||||||
|
using global::eShopOnContainers.Identity.Models;
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
CardHolderName = "Jhon Doe",
|
||||||
|
CardNumber = "1111-2222-33-4444",
|
||||||
|
CardType = 1,
|
||||||
|
City = "Seattle",
|
||||||
|
Country = "EEUU",
|
||||||
|
CountryCode = "91",
|
||||||
|
Email = "jdoe@eshop.com",
|
||||||
|
Expiration = "12/20",
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
LastName = "Doe",
|
||||||
|
Name = "Jhon",
|
||||||
|
PhoneNumber = "600 11 22 33",
|
||||||
|
UserName = "jdoe@eshop.com",
|
||||||
|
ZipCode = "56730",
|
||||||
|
State = "Washington",
|
||||||
|
Street = "Street..",
|
||||||
|
SecurityNumber = "256",
|
||||||
|
NormalizedEmail = "JDOE@ESHOP.COM",
|
||||||
|
NormalizedUserName = "JDOE@ESHOP.COM",
|
||||||
|
SecurityStamp = Guid.NewGuid().ToString("D")
|
||||||
|
};
|
||||||
|
|
||||||
|
user.PasswordHash = _passwordHasher.HashPassword(user, "eshopContainers.123");
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ namespace eShopOnContainers.Identity.Services
|
|||||||
|
|
||||||
public async Task<ApplicationUser> FindByUsername(string user)
|
public async Task<ApplicationUser> FindByUsername(string user)
|
||||||
{
|
{
|
||||||
return await _userManager.FindByNameAsync(user);
|
return await _userManager.FindByEmailAsync(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ValidateCredentials(ApplicationUser user, string password)
|
public async Task<bool> ValidateCredentials(ApplicationUser user, string password)
|
||||||
|
@ -15,6 +15,8 @@ using eShopOnContainers.Identity.Services;
|
|||||||
using eShopOnContainers.Identity.Configuration;
|
using eShopOnContainers.Identity.Configuration;
|
||||||
using IdentityServer4.Services;
|
using IdentityServer4.Services;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
namespace eShopOnContainers.Identity
|
namespace eShopOnContainers.Identity
|
||||||
{
|
{
|
||||||
@ -51,6 +53,7 @@ namespace eShopOnContainers.Identity
|
|||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
|
|
||||||
services.AddTransient<IEmailSender, AuthMessageSender>();
|
services.AddTransient<IEmailSender, AuthMessageSender>();
|
||||||
@ -102,29 +105,10 @@ namespace eShopOnContainers.Identity
|
|||||||
template: "{controller=Home}/{action=Index}/{id?}");
|
template: "{controller=Home}/{action=Index}/{id?}");
|
||||||
});
|
});
|
||||||
|
|
||||||
MigrateDb(app);
|
//Seed Data
|
||||||
}
|
var hasher = new PasswordHasher<ApplicationUser>();
|
||||||
|
new ApplicationContextSeed(hasher).SeedAsync(app, loggerFactory)
|
||||||
int retries = 0;
|
.Wait();
|
||||||
|
|
||||||
private void MigrateDb(IApplicationBuilder app)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var context = (ApplicationDbContext)app
|
|
||||||
.ApplicationServices.GetService(typeof(ApplicationDbContext));
|
|
||||||
using (context)
|
|
||||||
{
|
|
||||||
context.Database.Migrate();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
retries++;
|
|
||||||
if (retries < 2)
|
|
||||||
MigrateDb(app);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
//"DefaultConnection": "Server=identity.data;Database=aspnet-Microsoft.eShopOnContainers;User Id=sa;Password=Pass@word"
|
|
||||||
"DefaultConnection": "Server=127.0.0.1,5433;Database=aspnet-Microsoft.eShopOnContainers;User Id=sa;Password=Pass@word"
|
"DefaultConnection": "Server=127.0.0.1,5433;Database=aspnet-Microsoft.eShopOnContainers;User Id=sa;Password=Pass@word"
|
||||||
},
|
},
|
||||||
"MvcClient": "http://localhost:5100",
|
"MvcClient": "http://localhost:5001",
|
||||||
"SpaClient": "http://localhost:5104",
|
"SpaClient": "http://localhost:5104",
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"IncludeScopes": false,
|
"IncludeScopes": false,
|
||||||
|
@ -14,8 +14,8 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
|||||||
var host = new WebHostBuilder()
|
var host = new WebHostBuilder()
|
||||||
.UseKestrel()
|
.UseKestrel()
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
//.UseIISIntegration()
|
|
||||||
.UseUrls("http://0.0.0.0:5100")
|
.UseUrls("http://0.0.0.0:5100")
|
||||||
|
.UseIISIntegration()
|
||||||
.UseStartup<Startup>()
|
.UseStartup<Startup>()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
@ -3,14 +3,13 @@
|
|||||||
"windowsAuthentication": false,
|
"windowsAuthentication": false,
|
||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:5100",
|
"applicationUrl": "http://localhost:5001",
|
||||||
"sslPort": 0
|
"sslPort": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"IIS Express": {
|
"IIS Express": {
|
||||||
"commandName": "IISExpress",
|
"commandName": "IISExpress",
|
||||||
"launchBrowser": true,
|
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"CatalogUrl": "http://localhost:5101",
|
"CatalogUrl": "http://localhost:5101",
|
||||||
"OrderingUrl": "http://localhost:5102",
|
"OrderingUrl": "http://localhost:5102",
|
||||||
"BasketUrl": "http://localhost:5103",
|
"BasketUrl": "http://localhost:5103",
|
||||||
"IdentityUrl": "http://localhost:5105",
|
"IdentityUrl": "http://localhost:5000",
|
||||||
"CallBackUrl": "http://localhost:5100/",
|
"CallBackUrl": "http://localhost:5100/",
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"IncludeScopes": false,
|
"IncludeScopes": false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user