identityserver4简单配置

新建netcore 项目

安装nuget包
在这里插入图片描述

在ConfigureServices 中加入

services.AddIdentityServer().
                AddDeveloperSigningCredential().
                AddInMemoryClients(InMemoryConfig.GetClients()).
                AddInMemoryApiScopes(InMemoryConfig.GetApiScopes()).
                AddInMemoryApiResources(InMemoryConfig.GetApiResources());

InMemoryConfig代码如下

  public class InMemoryConfig
    {
        public static IEnumerable<IdentityResource> IdentityResources =>
        new IdentityResource[]
        {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
        };

        /// <summary>
        /// ApiResource 资源列表
        /// </summary>
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new[]
            {
            new ApiResource("Users", "获取用户信息API")
            {
                Scopes={ "scope1" }//必须
            }
        };
        }

        /// <summary>
        /// ApiScopes 作用域
        /// </summary>
        public static IEnumerable<ApiScope> GetApiScopes()
        {
            return new ApiScope[]
              {
            new ApiScope("scope1")
              };
        }

        /// <summary>
        /// Client 客户端
        /// </summary>
        public static IEnumerable<Client> GetClients()
        {
            return new[]
            {
            new Client
            {
                ClientId = "HomeJok.Authentication",                        //客户端唯一标识
                ClientName = "Authentication",                              //客户端名称
                ClientSecrets = new [] { new Secret("wintersir".Sha256()) },//客户端密码,进行了加密
                AllowedGrantTypes = GrantTypes.ClientCredentials,           //授权方式,客户端认证 ClientId+ClientSecrets
                AllowedScopes = new [] { "scope1" },                        //允许访问的资源
                Claims = new List<ClientClaim>(){
                    new ClientClaim(IdentityModel.JwtClaimTypes.Role,"Admin"),
                    new ClientClaim(IdentityModel.JwtClaimTypes.NickName,"WinterSir"),
                    new ClientClaim("email","641187567@qq.com")
                }
            }
        };
        }
    }

Configure中加入

 app.UseIdentityServer();

在这里插入图片描述
启动认证服务dotnet run urls=http://*:5000,用postman或者apifox 请求接口

http://localhost:5000/connect/token

grant_type:client_credentials
client_id:HomeJok.Authentication
client_secret:wintersir
在这里插入图片描述
可以用jwt工具验证一下token JWT链接1 JWT链接2
在这里插入图片描述
认证中心就可以了,下面新建webapi项目

api项目中的stratup 中 ConfigureServices 添加

  services.AddAuthentication("Bearer").AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:5000";//认证中心地质
                options.ApiName = "Users";//认证中心资源列表中的名称
                options.RequireHttpsMetadata = false;
            });

Configure中添加
app.UseAuthentication();//鉴权
在这里插入图片描述
然后在controller中的接口添加
[Authorize]
dotnet run urls=“http://localhost:8000”

最后用apifox请求接口
token错误时会报错401 ,
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢小亦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值