Re-factored and formatted OcelotApiGw project

This commit is contained in:
Rafsanul Hasan 2018-09-09 04:07:24 +06:00
parent 25cc0cf562
commit 4e774612ae
No known key found for this signature in database
GPG Key ID: FC57FD2D87BE60DD
2 changed files with 98 additions and 94 deletions

View File

@ -1,13 +1,10 @@
using System; using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using static Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions;
using static Microsoft.AspNetCore.Hosting.WebHostExtensions;
using IWebHost = Microsoft.AspNetCore.Hosting.IWebHost;
using Path = System.IO.Path;
using WebHost = Microsoft.AspNetCore.WebHost;
namespace OcelotApiGw namespace OcelotApiGw
{ {
@ -20,11 +17,12 @@ namespace OcelotApiGw
public static IWebHost BuildWebHost(string[] args) public static IWebHost BuildWebHost(string[] args)
{ {
IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args); var builder = WebHost.CreateDefaultBuilder(args);
builder.ConfigureServices(s => s.AddSingleton(builder)) builder
.ConfigureServices(s => s.AddSingleton(builder))
.ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json"))) .ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json")))
.UseStartup<Startup>(); .UseStartup<Startup>();
IWebHost host = builder.Build(); var host = builder.Build();
return host; return host;
} }
} }

View File

@ -1,11 +1,5 @@
using System; using Microsoft.AspNetCore.Builder;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CacheManager.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -29,17 +23,26 @@ namespace OcelotApiGw
var identityUrl = _cfg.GetValue<string>("IdentityUrl"); var identityUrl = _cfg.GetValue<string>("IdentityUrl");
var authenticationProviderKey = "IdentityApiKey"; var authenticationProviderKey = "IdentityApiKey";
services.AddCors(options => services
.AddCors(options =>
{ {
options.AddPolicy("CorsPolicy", options
builder => builder.AllowAnyOrigin() .AddPolicy(
"CorsPolicy",
builder =>
builder
.AllowAnyOrigin()
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials()); .AllowCredentials()
);
}); });
services.AddAuthentication() services
.AddJwtBearer(authenticationProviderKey, x => .AddAuthentication()
.AddJwtBearer(
authenticationProviderKey,
x =>
{ {
x.Authority = identityUrl; x.Authority = identityUrl;
x.RequireHttpsMetadata = false; x.RequireHttpsMetadata = false;
@ -49,18 +52,21 @@ namespace OcelotApiGw
}; };
x.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents() x.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents()
{ {
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
OnAuthenticationFailed = async ctx => OnAuthenticationFailed = async ctx =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{ {
int i = 0;
}, },
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
OnTokenValidated = async ctx => OnTokenValidated = async ctx =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{ {
int i = 0;
}, },
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
OnMessageReceived = async ctx => OnMessageReceived = async ctx =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{ {
int i = 0;
} }
}; };
}); });