要在ASP.NET Core v1.1中集成IdentityServer和Simple Injector,您需要执行以下步骤:
IdentityServerConfig.cs
的类,并添加以下代码:using IdentityServer4;
using IdentityServer4.Models;
using System.Collections.Generic;
public static class IdentityServerConfig
{
public static IEnumerable GetIdentityResources()
{
return new List
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
}
public static IEnumerable GetApiResources()
{
return new List
{
new ApiResource("api1", "My API"),
};
}
public static IEnumerable GetClients()
{
return new List
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
},
};
}
}
Startup.cs
文件中,配置IdentityServer和Simple Injector的集成。在ConfigureServices
方法中添加以下代码:public void ConfigureServices(IServiceCollection services)
{
// ... 其他配置代码 ...
// 添加IdentityServer
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
.AddInMemoryClients(IdentityServerConfig.GetClients())
.AddAspNetIdentity();
// 使用Simple Injector替换默认的依赖注入容器
var container = new Container();
container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
container.ConfigureAspNetCore(app);
services.AddSingleton(
new SimpleInjectorControllerActivator(container));
services.AddSingleton(
new SimpleInjectorViewComponentActivator(container));
services.UseSimpleInjectorAspNetRequestScoping(container);
// 将Simple Injector容器设置为默认服务提供程序
services.AddSingleton(container);
services.AddSingleton(
new SimpleInjectorDependencyResolver(container));
// ... 其他配置代码 ...
}
Configure
方法中启用IdentityServer和Simple Injector。添加以下代码:public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ... 其他配置代码 ...
// 使用IdentityServer中间件
app.UseIdentityServer();
// 使用Simple Injector中间件
app.UseSimpleInjectorAspNetRequestScoping(container);
// ... 其他配置代码 ...
}
这样,您就成功地将IdentityServer和Simple Injector集成到ASP.NET Core v1.1中了。请注意,以上代码示例仅供参考,您可能需要根据您的具体需求进行适当的调整和修改。
上一篇:ASP.Net Core UWP SignalR客户端HubConnectionBuilder.Build崩溃
下一篇:Asp.Net Core v3.1在最新的win iot v17763和Raspberry Pi 2上无法运行。