内置 DI
ASP.NET Core 内置 DI,当然你也可以使用别的 DI 实现。
换用 PostgreSQL
Startup 有注释的位置就是更改:
public void ConfigureServices(IServiceCollection services)
{
// Store connection string as a var
var connectionString = Configuration.GetConnectionString("DefaultConnection");
// Store assembly for migrations
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
// Replace DbContext database form SqLite in template to Postgres
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(connectionString));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.Configure<IISOptions>(iis =>
{
iis.AuthenticationDisplayName = "Windows";
iis.AutomaticAuthentication = false;
});
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
})
// Use out Postgres Database for storing configuration data
.AddConfigurationStore(configDb =>
{
configDb.ConfigureDbContext = db => db.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
// Use out Postgres Database for storing operational data
.AddOperationalStore(operationalDb =>
{
operationalDb.ConfigureDbContext = db => db.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddAspNetIdentity<ApplicationUser>();
if (Environment.IsDevelopment())
{
builder.AddDeveloperSigningCredential();
}
else
{
throw new Exception("need to configure key material");
}
services.AddAuthentication()
.AddGoogle(options =>
{
options.ClientId = "708996912208-9m4dkjb5hscn7cjrn5u0r4tbgkbj1fko.apps.googleusercontent.com";
options.ClientSecret = "wdfPY6t8H8cecgjlxud__4Gh";
});
}
ApplicationUser 是我们回头要自定义使用的。
它继承自 IdentityUser,IdentityUser 是 Microsoft.AspNetCore.Identity.IdentityUser 的默认实现。
appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=5432;Database=archdb_dev;User Id=arch_agent;Password=~pass123~;"
}
}