初步接触认证授权,记录一下自己的学习:
分两个模块:
1、AccessToken:生成token;
2、Increse:测试模块;
Access需配置IdentityServer4服务:
引用包:
startup.cs添加服务
builder.Services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiScopes(Config.ApiScopes)
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.Clients);
还有这个:
app.UseIdentityServer();
使用token的模块配置:
先导包
然后在startup里面添加授权:
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
//地址为id4认证中心地址,这里为AccessToken的地址
options.Authority = "https://localhost:5001";
options.RequireHttpsMetadata = false;
options.Audience = "api1";
});
还有这:
app.UseAuthentication();
app.UseAuthorization();
运行项目进入https://localhost:5001/connect/token
测试接口:
未有token时:
加上token后
大功告成,接下来可以考虑使用swagger添加authoricazion,和前端结合使用