作为消费者和提供商的IS4 webapi服务器

问题描述:

我有一个.net核心Web API,它跟随着IS4的this example。只要IS4服务只是作为令牌的提供者,它就可以正常工作。我想使用的服务作为消费者也一样,在服务本身暴露了一些端点这样的:作为消费者和提供商的IS4 webapi服务器

[Route("[controller]")] 
public class ValuesController : ControllerBase 
{ 
    [HttpGet("authorised"), Authorize] 
    public IActionResult Authorised() 
    { 
     return new JsonResult("hello world"); 
    } 

    [HttpGet("unauthorised")] 
    public IActionResult Unauthorised() 
    { 
     return new JsonResult("unauth hello world"); 
    } 
} 

unauthorised呼叫完美的作品,但authorised呼叫不会在所有的工作。即使更蹊跷的是,邮差返回404 Not Found

的IS4逻辑(Startup.cs)看起来如下:

public class Startup 
{ 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvcCore() 
      .AddAuthorization() 
      .AddJsonFormatters(); 

     services.AddAuthentication("Bearer") 
      .AddIdentityServerAuthentication(options => 
      { 
       options.Authority = "http://localhost:5000"; 
       options.RequireHttpsMetadata = false; 

       options.ApiName = "api1"; 
       options.ApiSecret = "secret"; 
      }); 

     // configure identity server with in-memory stores, keys, clients and scopes 
     services.AddIdentityServer() 
      .AddDeveloperSigningCredential() 
      .AddInMemoryApiResources(Config.GetApiResources()) 
      .AddInMemoryClients(Config.GetClients()); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
     } 

     //app.UseAuthentication(); 
     app.UseIdentityServer(); // does .UseAuthentication inside 
     app.UseMvc(); 
    } 
} 

古怪此用来工作其使用.NET芯1.1内置先前IS4样品中细。任何帮助将不胜感激。

这应该适合你。

public class Startup 
{ 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvcCore() 
      .AddAuthorization() 
      .AddJsonFormatters(); 

     // Use default auth scheme (cookies) 
     services.AddAuthentication(options => { 
       options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
       options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; 
      }) 
      .AddIdentityServerAuthentication("Bearer", options => 
      { 
       options.Authority = "http://localhost:5000"; 
       options.RequireHttpsMetadata = false; 

       options.ApiName = "api1"; 
       options.ApiSecret = "secret"; 
      }); 

      services.AddAuthorization(options => 
      {     
       options.AddPolicy("ApiPolicy", policy => 
       { 
        policy.AddAuthenticationSchemes("Bearer"); 
        policy.RequireAuthenticatedUser(); 
       }); 
      }); 

     // configure identity server with in-memory stores, keys, clients and scopes 
     services.AddIdentityServer() 
      .AddDeveloperSigningCredential() 
      .AddInMemoryApiResources(Config.GetApiResources()) 
      .AddInMemoryClients(Config.GetClients()); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
     } 

     //app.UseAuthentication(); 
     app.UseIdentityServer(); // does .UseAuthentication inside 
     app.UseMvc(); 
    } 
} 

现在在你的控制器中的一个,使用一个控制器方法或控制器本身的属性[Authorize("ApiPolicy")]