MVC .NET CORE 学习之路三:搭建.net core权限认证的环境(IdentityServer或jwt)

本文详细介绍了如何在ASP.NET Core中配置IdentityServer4,包括设置客户端、API资源、证书,并展示了如何在Web API中启用JWT验证。通过步骤演示了生成证书、配置 Startup.cs 文件以及验证JWT Token的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建ASP.NET Core Web 应用程序,程序文件如下图

在这里插入图片描述
引用以下包

2.新建config类配置客户端列表

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

        /// <summary>
        /// api范围
        /// </summary>
         public static IEnumerable<ApiScope> ApiScopes => 
            new ApiScope[]           
            {      
                new ApiScope("api")           
            };

        /// <summary>
        /// api资源
        /// </summary>
        public static IEnumerable<ApiResource> ApiResources => 
            new ApiResource[]      
            {      
                new ApiResource("api","#api")      
                {                
                   Scopes = { "api" }           
                }
            };

        public static IEnumerable<Client> GetClientConfigList()
        {                
            var appInfo = Account.Instance.GetApps(); //这个是从数据库获取已配置的客户端。     
            List<Client> Clients = new List<Client>();            
            foreach (var item in appInfo)            
            {                
                Client client = new Client();                
                client.ClientId = item.App_ID;               
                client.AllowedGrantTypes = GrantTypes.ClientCredentials;//授权类型,这里使用的是客户端凭证模式               
                client.ClientSecrets.Add(new Secret(item.App_Key.Sha256()));                
                client.All
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值