2017-10-12 4 views
0

Ich habe eine .net-Core-Web-API, die this example von IS4 folgt. Es funktioniert alles gut, solange der IS4-Dienst einfach als Anbieter von Tokens fungiert. Ich möchte den Dienst als Verbraucher verwenden, wie sie in den Dienst selbst einige Endpunkte wie dies macht:IS4 Webapi-Server als Verbraucher und Provider

[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"); 
    } 
} 

Die unauthorised Anrufe funktioniert perfekt, aber die authorised Anrufe überhaupt nicht funktionieren. Noch seltsamer ist, dass Postman 404 Not Found zurückgibt.

Die IS4 Logik (Startup.cs) sieht wie folgt:

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(); 
    } 
} 

Weirdly dies in den vorherigen IS4 Proben gut zu funktionieren verwendet, die .NET gebaut wurden Kern 1.1. Jede Hilfe wird geschätzt.

Antwort

0

Dies sollte für Sie arbeiten.

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(); 
    } 
} 

nun in einem Ihrem Controller, verwenden Sie das Attribut [Authorize("ApiPolicy")] auf einer Controller-Methode oder die Steuerung selbst.

Verwandte Themen