Created
October 6, 2020 15:20
-
-
Save rahulsahay19/d8137cb32878807bfc7256e45463a0e0 to your computer and use it in GitHub Desktop.
startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.IdentityModel.Tokens; | |
using Ocelot.Cache.CacheManager; | |
using Ocelot.DependencyInjection; | |
using Ocelot.Middleware; | |
namespace OcelotGateway | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddAuthentication(option => | |
{ | |
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}).AddJwtBearer(option => | |
{ | |
option.RequireHttpsMetadata = true; | |
option.SaveToken = true; | |
option.TokenValidationParameters = new TokenValidationParameters | |
{ | |
IssuerSigningKey = | |
new SymmetricSecurityKey( | |
Encoding.ASCII.GetBytes(Configuration.GetSection("JWTSettings:SecretKey").Value)), | |
ValidateIssuerSigningKey = true, | |
ValidateIssuer = false, | |
ValidateAudience = false | |
}; | |
}); | |
services.AddOcelot().AddCacheManager(option => option.WithDictionaryHandle()); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapGet("/", | |
async context => { await context.Response.WriteAsync("Ocelot API Gateway"); }); | |
}); | |
app.UseOcelot().Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment