您可以向托管identityserver4的应用程序添加更多api端点。
您通常希望通过它们所托管的identityserver实例来保护这些api。这不是问题。只需将令牌验证处理程序添加到主机(请参阅此处):
public void configureservices(iservicecollection services) { services.addmvc(); // details omitted services.addidentityserver(); services.addauthentication() .addidentityserverauthentication("token", isauth => { isauth.authority = "base_address_of_identityserver"; isauth.apiname = "name_of_api"; }); }
在您的api上,您需要添加[authorize]
属性并显式引用您要使用的身份验证方案(在此示例中token
,您可以选择您喜欢的任何名称):
public class testcontroller : controllerbase { [route("test")] [authorize(authenticationschemes = "token")] public iactionresult get() { var claims = user.claims.select(c => new { c.type, c.value }).toarray(); return ok(new { message = "hello api", claims }); } }
如果要从浏览器调用该api,则还需要配置cors(请参阅此处)。
43.1 发现
如果需要,您还可以将端点添加到发现文档中,例如:
services.addidentityserver(options => { options.discovery.customentries.add("custom_endpoint", "~/api/custom"); })
github地址
黄山市民网:https://www.huangshanshimin.com/