使用identityserver4保护多个API

使用identityserver4保护多个API,identityserver4,identityserver3,Identityserver4,Identityserver3,我想只使用一个identityserver4应用程序来保护我的所有API 我的sirst资源api和客户端应用程序: CustomerManagementApi CustomerManagement.JavascriptApplication CustomerManagement.iOSApp CustomerManagement.AndroidApp 我的第二个资源api和应用程序: 人力资源API HumanResource.mvc应用程序 我的其他资源api和应用程序: 仪表板A

我想只使用一个identityserver4应用程序来保护我的所有API

我的sirst资源api和客户端应用程序:

  • CustomerManagementApi
  • CustomerManagement.JavascriptApplication
  • CustomerManagement.iOSApp
  • CustomerManagement.AndroidApp
我的第二个资源api和应用程序:

  • 人力资源API
  • HumanResource.mvc应用程序
我的其他资源api和应用程序:

  • 仪表板API
  • Dashboard.angular应用程序
我只想创建一个IdentityServer4并保护我的资源(DashboardApi、HumanResourceApi、CustomerManagementApi),我想将我的客户端应用程序保存在同一个IdentityServer4应用程序上


这可能吗?我应该在identityserver上创建不同的ApiResources和作用域吗?我如何才能做到这一点?

是的,这是可能的,因为IdentityServer4允许您定义资源API、客户端应用程序、用户、作用域,并且您可以使用内存中的数据为初始测试或甚至其他存储机制(例如实体框架)配置这些数据

这里的解释并不简单,但在官方网站上,您可以做一些快速入门来了解更多信息

您可以在上面看到资源API、客户端应用程序、内存中用户(使用Config.cs类)的一些配置示例,以便了解如何简单地启动:

资源API:客户端希望访问的受保护API

   public static IEnumerable<ApiResource> GetApis()
   {
        return new List<ApiResource>
        {
            new ApiResource("CustomerManagementApi", "My CustomerManagementApi"),
            new ApiResource("DashboardApi", "My DashboardApi"),
            // others ...
        };
   }
公共静态IEnumerable getAPI()
{
返回新列表
{
新的ApiResource(“CustomerManagementApi”、“我的CustomerManagementApi”),
新的API资源(“DashboardApi”、“我的DashboardApi”),
//其他。。。
};
}
客户端:希望访问资源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 = { "CustomerManagementApi" }
        }
    };
}
public静态IEnumerable GetClients()
{
返回新列表
{
新客户
{
ClientId=“客户端”,
//无交互用户,请使用clientid/secret进行身份验证
AllowedGrantTypes=GrantTypes.ClientCredentials,
//身份验证的秘密
客户秘密=
{
新密码(“Secret.Sha256())
},
//客户端有权访问的作用域
AllowedScopes={“CustomerManagementApi”}
}
};
}
用户:希望访问某些资源的最终用户

public static List<TestUser> GetUsers()
{
    return new List<TestUser>
    {
        new TestUser
        {
            SubjectId = "1",
            Username = "alice",
            Password = "password"
        },
        new TestUser
        {
            SubjectId = "2",
            Username = "bob",
            Password = "password"
        }
    };
}
公共静态列表GetUsers()
{
返回新列表
{
新测试用户
{
SubjectId=“1”,
Username=“alice”,
Password=“Password”
},
新测试用户
{
SubjectId=“2”,
Username=“bob”,
Password=“Password”
}
};
}

很抱歉给一篇旧文章添加了一个qn,但这是直接相关的。一旦有了API资源,它们如何映射到API控制器,比如在aspnetcore中?“授权”属性中是否有链接?如何将“DashboardApi”api资源链接到实际的api?