.net IdentityServer4的使用

初步接触认证授权,记录一下自己的学习:

分两个模块:

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,和前端结合使用

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值