Last active
July 12, 2019 18:50
-
-
Save baughmann/b9b921cf5761e6214b8e5fdd039aa706 to your computer and use it in GitHub Desktop.
Application Startup
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; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Threading.Tasks; | |
using Couchbase.Extensions.DependencyInjection; | |
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.IdentityModel.Tokens; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer; | |
namespace MyApp | |
{ | |
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | |
// In production, the React files will be served from this directory | |
services.AddSpaStaticFiles(configuration => | |
{ | |
configuration.RootPath = "ClientApp/build"; | |
}); | |
// Authentication | |
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | |
.AddJwtBearer(options => | |
{ | |
options.TokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidIssuer = Configuration.GetSection("Authentication").GetValue<string>("Issuer"), | |
ValidateIssuerSigningKey = true, | |
ValidateIssuer = true, | |
ValidateLifetime = true, | |
ValidAudience = Configuration.GetSection("Authentication").GetValue<string>("Audience"), | |
ValidateAudience = true | |
}; | |
}); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
app.UseAuthentication(); | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Error"); | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseSpaStaticFiles(); | |
app.UseSignalR(routes => | |
{ | |
routes.MapHub<CaseHub>("/caseHub"); | |
}); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller}/{action=Index}/{id?}"); | |
}); | |
app.UseSpa(spa => | |
{ | |
spa.Options.SourcePath = "ClientApp"; | |
if (env.IsDevelopment()) | |
{ | |
spa.UseReactDevelopmentServer(npmScript: "start"); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment