标签:static lse code 集成 roo shel 需要 The configure
首先创建3个项目,这3个项目将做为我们学习 IdentityServer4 的基础项目,项目框架全部使用 .NET CORE 3.1。

MicroShell.IdentityServer4.Server : 5000
MicroShell.IdentityServer4.Api : 5001
MicroShell.IdentityServer4.Mvc : 5002
在项目 MicroShell.IdentityServer4.Server 中添加 Nuget 包:IdentityServer4,笔者使用的是 4.1.2 版本。
Install-Package IdentityServer4 -Version 4.1.2
ConfigureServices 方法中添加 IdentityServer 的依赖服务。
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client1",
// 没有交互性用户,使用 clientid/secret 实现认证。
AllowedGrantTypes = GrantTypes.ClientCredentials,
// 用于认证的密码
ClientSecrets =
{
new Secret("secret".Sha256())
},
// 客户端有权访问的范围(Scopes)
AllowedScopes = { "api1" }
}
};
}
public static IEnumerable<ApiScope> GetApiScopes()
{
return new List<ApiScope>
{
new ApiScope("api1", "我的 API"),
};
}
做为快速入门篇,遵循一切从简的原则, Client 、签名都使用内存持久化模型,数据库持久化我们放到后面去讲。 准备好 Client 和 ApiScope 后开始注入它们的依赖服务。
services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryClients(Startup.GetClients()) .AddInMemoryApiScopes(Startup.GetApiScopes()) ;
Configure 方法中注入 IdentityServer 中间件。
app.UseRouting(); app.UseIdentityServer(); // 要放在 UseRouting 的后面

在项目 MicroShell.IdentityServer4.Api 中添加 Nuget 包:IdentityServer4.AccessTokenValidation,笔者使用的是 3.0.1 版本。
IdentityServer4.AccessTokenValidation -Version 3.0.1
在 ConfigureServices 方法中添加认证服务。
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", o => {
o.Authority = "http://localhost:5000";
o.RequireHttpsMetadata = false;
o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateAudience = false
};
})
;
在 Configure 方法中添加中间件。
app.UseRouting(); // 认证和授权中间件要放到路由中间后面 app.UseAuthentication(); app.UseAuthorization();
[ApiController]
[Route("[controller]")]
[Authorize]
public class HomeController : ControllerBase
{
public HomeController()
{
}
[HttpGet]
public string Get()
{
return "极限编程网";
}
}

入门篇我们使用的 Client 是 ClientCredentials(客户端凭证模式),该模式因为没有用户参与,不适合 MVC 项目场景,所以笔者省略了 MVC 项目下的集成。
本文转载自:https://limitcode.com/detail/606d4d61d9118c3cd4168786.html
标签:static lse code 集成 roo shel 需要 The configure
原文地址:https://www.cnblogs.com/limitcode/p/14639513.html