当前位置: 移动技术网 > IT编程>开发语言>.net > 第43章 添加更多API端点 - Identity Server 4 中文文档(v1.0.0)

第43章 添加更多API端点 - Identity Server 4 中文文档(v1.0.0)

2019年04月29日  | 移动技术网IT编程  | 我要评论

神医皇妃 柳风拂叶,当初求种像条狗 如今撸完嫌人丑,超级修圣传奇

您可以向托管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地址

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网