Saturday, May 4, 2019

Integrating Authentication Server with API Server

Hey guys,I've been seeing this Pluralsight guide how on to use IdentityServer.So the insstructor opened a ASP.NET CORE app for Identity Server and showed how to integrate the projects together.Everything seems fine, except with I send the generated token for Authorization I get this message:

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5000/home/Hello

'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.9\System.Threading.Tasks.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Route matched with {action = "Hello", controller = "Home"}. Executing action SwapExperimental.Controllers.HomeController.Hello (Swap.Api)

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed.

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.

Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes ().

IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler:Information: AuthenticationScheme: Bearer was challenged.

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action SwapExperimental.Controllers.HomeController.Hello (Swap.Api) in 21.7753ms

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 62.1688ms 401

The thread 0xeac has exited with code 0 (0x0).

Well, Is there any way I can get more information? I feel like this is pretty vague.

And second, Why is it happening?Here's my startup.cs and Program.cs of both projects :

startup.cs API

namespace SwapExperimental { public class Startup { private IConfiguration Configuration { get; set; } public Startup(IHostingEnvironment environment) { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.SetBasePath(environment.ContentRootPath); builder.AddJsonFile("appsettings.json"); builder.AddEnvironmentVariables(); Configuration = builder.Build(); } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(authOptions => { authOptions.DefaultScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; authOptions.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; }).AddIdentityServerAuthentication(options => { options.RequireHttpsMetadata = true; options.Authority = "http://localhost:5001"; options.ApiName = "Swap"; }); services.AddSingleton(Configuration); services.AddScoped<ITokenSetData, SqlTokensetData>(); services.AddDbContext<DbContextTokenSet>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseFileServer(); app.UseMvc(ConfigureRoutes); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.Run(ctx => ctx.Response.WriteAsync("NotFound")); } private void ConfigureRoutes(IRouteBuilder routerBuilder) { routerBuilder.MapRoute("Default", "{controller=Home}/{action=Index}/{id?}"); } } } 

Program.cs API :

namespace SwapExperimental { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); }

 public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseUrls("http://127.0.0.1:5000") .Build(); } } 

AuthServer Startup:

namespace Swap.AuthServer { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services .AddIdentityServer() .AddSigningCredential(new X509Certificate2(@"D:\Programming Projects\Swap\swap.pfx", "password")) .AddInMemoryApiResources(InMemoryConfiguration.ApiResources()) .AddInMemoryClients(InMemoryConfiguration.Clients()) .AddTestUsers(InMemoryConfiguration.Users().ToList()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); app.UseIdentityServer(); } } } 

Program.cs :

namespace Swap.AuthServer { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls("https://localhost:5001") .UseStartup<Startup>(); } } 

Is everything fine with it? Why can't I authenticate the token given by Authserver.Thanks :]

Integrating Authentication Server with API Server Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team