Skip to content

Instantly share code, notes, and snippets.

@wayne-o
Created February 19, 2018 14:46
Show Gist options
  • Save wayne-o/874dcf16e9e659abfccdbdf8436f4aff to your computer and use it in GitHub Desktop.
Save wayne-o/874dcf16e9e659abfccdbdf8436f4aff to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using XXX.PermissionsService.Data;
using XXX.PermissionsService.Models;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using XXX.Infrastructure.Extensions;
namespace XXX.PermissionsService
{
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration configuration)
{
//ConfigureAutoMapper();
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)
{
ConfigureMvc(services);
ConfigureIdentity(services);
ConfigureDbContexts(services);
services.AddTransient<ILoggerFactory, LoggerFactory>();
services.AddTransient(typeof(ILogger<>), typeof(Logger<>));
}
private void ConfigureDbContexts(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
private static void ConfigureMvc(IServiceCollection services)
{
services.AddMvcCore()
.AddJsonOptions(options =>
{
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
})
.AddAuthorization()
.AddJsonFormatters();
}
private void ConfigureIdentity(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = "Bearer";
})
.AddIdentityServerAuthentication(options =>
{
options.Authority = Configuration.GetSection("IdentityServer")["Url"];
options.ApiName = "XXX.permissions";
options.ApiSecret = "secret".Sha256();
options.RequireHttpsMetadata = false;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var logger = loggerFactory.CreateLogger<Startup>();
logger.LogInformation($"Starting application in {env.EnvironmentName} environment");
app.UseCors(builder =>
{
builder.WithOrigins("https://xxx.com:4200", "https://xxx.com", "http://xxx.com:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment