【问题标题】:Access token for API controller inside Identity server itself身份服务器本身内部 API 控制器的访问令牌
【发布时间】:2017-07-12 10:43:53
【问题描述】:

我创建了一个身份服务器 4 项目和一个 mvc 客户端项目。身份验证流程按预期工作。我在与身份服务器相同的项目中添加了一个 API 控制器,我想从 mvc 客户端访问这个 api 资源。基本上,我需要身份服务器中间件和身份服务器项目中的令牌验证中间件。

【问题讨论】:

  • 有什么原因不能将 ID4 用作身份验证服务器和令牌服务器?

标签: asp.net-core-mvc identityserver4 thinktecture-ident-server


【解决方案1】:

如果您还没有,请将这些 Nuget 包添加到您已经建立的 IdentityServer 应用程序/站点:

IdentityServer4.AccessTokenValidation
Microsoft.AspNetCore.Mvc

将另一个 Api 资源添加到您的资源列表中:

public static IEnumerable<ApiResource> GetApiResources()
{
    return new List<ApiResource>
    {
        new ApiResource("api1", "My API"), 
        new ApiResource("api2", "IdentityServer API")
    };
}

更新您的客户端配置以允许 api2:

public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client
        {
            ClientId = "mvc",

            ... omitted

            AllowedScopes = new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                "api2"
            }
        }
    };
}

在启动时IdentityServer的Configure方法中添加:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:5000",
    RequireHttpsMetadata = false,

    ApiName = "api2"
});

【讨论】:

  • 我做过类似的事情。事实证明我在访问令牌中与观众开玩笑:(。它现在正在工作。谢谢:)
  • 您能否从客户端将您的消费代码添加到此示例中。那是我不理解的部分?
猜你喜欢
  • 2017-03-08
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 2014-12-01
  • 1970-01-01
相关资源
最近更新 更多