139 lines
5.2 KiB
C#
Raw Normal View History

2018-01-26 16:09:36 +00:00
using System;
using System.Collections.Generic;
2018-01-30 08:50:44 +00:00
using System.IdentityModel.Tokens.Jwt;
2018-01-26 16:09:36 +00:00
using System.Linq;
using System.Threading.Tasks;
2018-01-30 08:50:44 +00:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2018-01-26 16:09:36 +00:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
2018-01-29 15:58:08 +00:00
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http;
2018-01-26 16:09:36 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2018-02-15 17:30:39 +01:00
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services;
2018-01-26 16:09:36 +00:00
using Swashbuckle.AspNetCore.Swagger;
2018-02-15 17:30:39 +01:00
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
2018-01-26 16:09:36 +00:00
{
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)
{
2018-01-29 15:58:08 +00:00
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IHttpClient, StandardHttpClient>();
services.AddTransient<ICatalogService, CatalogService>();
services.AddTransient<IBasketService, BasketService>();
services.AddTransient<IOrderApiClient, OrderApiClient>();
2018-01-29 15:58:08 +00:00
services.AddOptions();
services.Configure<UrlsConfig>(Configuration.GetSection("urls"));
2018-01-26 16:09:36 +00:00
services.AddMvc();
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
2018-02-27 17:29:37 +01:00
Title = "Shopping Aggregator for Mobile Clients",
2018-01-26 16:09:36 +00:00
Version = "v1",
2018-02-27 17:29:37 +01:00
Description = "Shopping Aggregator for Mobile Clients",
2018-01-26 16:09:36 +00:00
TermsOfService = "Terms Of Service"
});
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
Scopes = new Dictionary<string, string>()
{
2018-02-27 17:29:37 +01:00
{ "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" }
2018-01-26 16:09:36 +00:00
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
2018-01-30 08:50:44 +00:00
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
var identityUrl = Configuration.GetValue<string>("urls:identity");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
2018-02-15 17:30:39 +01:00
options.Audience = "mobileshoppingagg";
2018-01-30 08:50:44 +00:00
options.Events = new JwtBearerEvents()
{
OnAuthenticationFailed = async ctx =>
{
int i = 0;
},
OnTokenValidated = async ctx =>
2018-01-30 08:50:44 +00:00
{
int i = 0;
}
};
});
2018-01-26 16:09:36 +00:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var pathBase = Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase))
{
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
app.UsePathBase(pathBase);
}
app.UseCors("CorsPolicy");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
2018-01-26 16:09:36 +00:00
app.UseMvc();
app.UseSwagger().UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
2018-02-15 17:30:39 +01:00
c.ConfigureOAuth2("Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregatorwaggerui", "", "", "Purchase BFF Swagger UI");
2018-01-26 16:09:36 +00:00
});
}
}
}