下面的代码可以工作,但有很多重复,我想知道我是否可以四处走动。
如果在开发环境中,我希望能够使用.AddDeveloperSigningCredential(false),否则设置证书集合对象并使用.AddSigningCredential(certCollection[0])。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<RSNROAuthContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("RSNRAccountDB")));
services.AddScoped(typeof(IUserProfileRepository), typeof(UserProfileRepository));
services.AddIdentity<User, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<RSNROAuthContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<IProfileService, ProfileService>();
services.AddMvc();
string connectionString = Configuration.GetConnectionString("RSNRAccountDB");
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
if (Env.IsDevelopment())
{
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential(false)
.AddAspNetIdentity<User>()
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddProfileService<ProfileService>();
}
else
{
X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, Configuration["WEBSITE_LOAD_CERTIFICATES"], false);
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddSigningCredential(certCollection[0])
.AddAspNetIdentity<User>()
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddProfileService<ProfileService>();
}
services.Configure<AuthMessageSenderOptions>(Configuration);
services.Configure<ClientOptions>(Configuration);
}发布于 2018-03-14 15:11:24
在if和else分支中有大量的重复代码可以被提取。假设services.XXX()方法使用的是fluent样式,并且每个调用都返回一个IServiceCollection,则如下所示
IIdentityServerBuilder serverBuilder = services.AddIdentityServer();
if (Env.IsDevelopment())
{
// configure identity server with in-memory stores, keys, clients and scopes
serverBuilder.AddDeveloperSigningCredential(false);
}
else
{
X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, Configuration["WEBSITE_LOAD_CERTIFICATES"], false);
// configure identity server with in-memory stores, keys, clients and scopes
serverBuilder.AddSigningCredential(certCollection[0]);
}
serverBuilder.AddAspNetIdentity<User>()
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly("RSNR.OAuth.DAL"));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddProfileService<ProfileService>();https://codereview.stackexchange.com/questions/189562
复制相似问题