身份服务器本身的API控制器访问令牌

问题描述:

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

+0

是否有理由不能使用ID4作为身份验证服务器和令牌服务器? –

如果您还没有准备好,这些的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的配置方法d in Startup地址:

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

    ApiName = "api2" 
}); 
+0

我做了类似的事情。事实证明,我正在与观众在访问令牌:(它现在工作了。谢谢:) – madCode