ASP.NET Core技术--使用客户端凭证保护API接口
2023-06-13 09:17:24 时间
将IdentityServer4部署到应用中具备如下特点:
1)、认证服务
2)、单点登陆
3)、API访问控制
4)、联合网关
5)、专注于定制
6)、成熟的开源系统
7)、免费和商业支持
其中API访问控制是IdentityServer4的重要用途之一。换言之,它可以是实现对API接口的保护。具体的步骤如下:
创建空项目
dotnet new -i IdentityServer4.Templates
dotnet new is4empty -n IdentityServer
Install-Package IdentityServer4
定义API资源
public static IEnumerable<ApiResource> GetApis()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
}
定义客户端
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client",
// no interactive user, use the clientid/secret for
authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
}
};
}
配置IdentityServer服务
public void ConfigureServices(IServiceCollection services) { var builder = services.AddIdentityServer() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryApiResources(Config.GetApis()) .AddInMemoryClients(Config.GetClients()); }
基于策略的授权
参考microsoft官网:
https://learn.microsoft.com/zh-cn/aspnet/core/security/authorization/policies?view=aspnetcore-7.0
相关文章
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- 《ASP.NET Core 6框架揭秘》勘误
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
- 【愚公系列】2022年11月 .NET CORE工具案例-.NET Core执行JavaScript
- 【特别的骚气】asp.net core运行时注入服务,实现类库热插拔
- asp.net core 成为构建企业web应用首选
- ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
- 【愚公系列】2022年12月 .NET CORE工具案例-.NET Core使用ExcelMapper
- 【愚公系列】2023年01月 .NET CORE工具案例-RedLock.net实现分布式锁
- 在 Asp.Net Core 中什么是认证和授权
- 【愚公系列】2023年02月 .NET CORE工具案例-Workflow-Core轻量级工作流引擎(简单使用)
- 【愚公系列】2023年02月 .NET CORE工具案例-Workflow-Core轻量级工作流引擎(流程事件)
- 正式开始学习ASP.NET Core 6 Razor Pages 介绍
- .net让Oracle与ASP.NET实现无缝连接(oracleasp)
- .net安装 Linux 上的 ASP.NET 环境(linux安装asp)
- asp.net(C#)动态添加非ASP的标准html控件(如添加Script标签)
- asp.net用户控件读取以及赋值
- ASP.NET服务器路径和一般资源调用
- asp.net中对象失去焦点时自动提交数据V2
- 基于ASP.NET的lucene.net全文搜索实现步骤
- ASP.NET中下载文件的几种实例代码
- asp.net中System.Timers.Timer的使用方法
- ASP.NET中UpdatePanel与jQuery同时使用所遇问题解决
- asp.net中调用Office来制作3D统计图的实例代码
- ASP.NET过滤HTML字符串方法总结
- 使用asp.net改变网页上图片颜色比如灰色变彩色