我正在使用IdentityServer3进行授权,我需要将其配置调整到不同的环境:dev,staging等。所以我需要可配置的重定向URL,证书等,但我找不到实现它的方法。据我所知,IS3中没有管理员用户界面,也没有计划。我是否需要创建自己的配置系统?
答案 0 :(得分:2)
没有找到任何适当的解决方案。最后创建自定义config.json并从那里读取设置。
我在这里留下一些代码。以防万一...
public class AuthConfiguration
{
private static readonly Lazy<AuthConfiguration> _instance = new Lazy<AuthConfiguration>(LoadConfig);
public static AuthConfiguration Instance
{
get { return _instance.Value; }
}
private AuthConfiguration()
{
}
private static AuthConfiguration LoadConfig()
{
string jsonString;
using (var r = new StreamReader(HttpRuntime.BinDirectory + "auth.config.json"))
{
jsonString = r.ReadToEnd();
}
return JsonConvert.DeserializeObject<AuthConfiguration>(jsonString);
}
public string AuthServerUrl { get; set; }
public AuthCertificate Certificate { get; set; }
public string[] CorsAllowedOrigins { get; set; }
public Dictionary<string, AuthClient> Clients { get; set; }
#region Helper classes
public class AuthCertificate
{
public string File { get; set; }
public string Password { get; set; }
}
public class AuthClient
{
public List<string> RedirectUris { get; set; }
public List<string> PostLogoutRedirectUris { get; set; }
public string[] Secrets { get; set; }
}
#endregion Helper classes
}
答案 1 :(得分:0)
我试图自己做,我得到了这个结论。实际上你只能编辑&#34;核心&#34;接口,这样做的方法是使用属性&#34; ViewService&#34;在你的管道中,像这样:
var factory = new IdentityServerServiceFactory();
factory.ViewService = new CustomView();
app.Map("/identity", id => {
id.UseIdentityServer(new IdentityServerOptions {
SiteName = "Demo Identity Server",
IssuerUri = (string)ConfigurationManager.AppSettings["options.issuerUri"],
Factory = factory,
SigningCertificate = LoadCertificate()
});
});
CustomView实现了IViewService,您必须在此处实现自定义方法以编辑视图。
(Configure是一种扩展方法,我实现整个管道定制,执行SOLID原则并解除责任)
public void Configuration(IAppBuilder app) {
string connectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
var factory = new IdentityServerServiceFactory();
factory.ViewService = new CustomView();
app.Map("/identity", id => {
id.UseIdentityServer(new IdentityServerOptions {
SiteName = "Demo Identity Server",
IssuerUri = (string)ConfigurationManager.AppSettings["options.issuerUri"],
Factory = factory,
SigningCertificate = LoadCertificate()
});
});
app.Map("/admin", adminApp => {
adminApp.UseIdentityManager(new IdentityManagerOptions() {
Factory = new IdentityManagerServiceFactory().Configure(connectionString)
});
});
}
X509Certificate2 LoadCertificate() {
//Test certificate sourced from https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/Certificates
return new X509Certificate2(
string.Format(@"{0}\bin\{1}", AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["signing-certificate.name"]),
(string)ConfigurationManager.AppSettings["signing-certificate.password"]);
}
}
配置扩展方法代码:
public static class IdentityServerServiceFactoryExtensions {
public static IdentityServerServiceFactory Configure(this IdentityServerServiceFactory factory, string connectionString) {
var serviceOptions = new EntityFrameworkServiceOptions { ConnectionString = connectionString };
factory.RegisterOperationalServices(serviceOptions);
factory.RegisterConfigurationServices(serviceOptions);
//factory.RegisterClientStore(serviceOptions);
factory.Register(new Registration<Context>(resolver => new Context(connectionString)));
factory.Register(new Registration<UserStore>());
factory.Register(new Registration<UserManager>());
factory.UserService = new Registration<IUserService, IdentityUserService>();
return factory;
}
}
但是当我试图为&#34; / admin&#34;做同样的事情时结束点我没有找到任何帮助或方式来定制这个。我在github项目中询问了这个问题,但我还没有得到任何反馈。
希望这个帮助