Skip to content

Instantly share code, notes, and snippets.

@tidusjar
Created September 18, 2017 15:29
Show Gist options
  • Save tidusjar/02cdaaf65197396efaa88ea0efa3876b to your computer and use it in GitHub Desktop.
Save tidusjar/02cdaaf65197396efaa88ea0efa3876b to your computer and use it in GitHub Desktop.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// ... Stuff
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments(new PathString("/api")))
{
// Let's check if this is an API Call
if (context.Request.Headers["ApiKey"].Any())
{
// validate the supplied API key
// Validate it
if (!valid)
{
context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
await context.Response.WriteAsync("Invalid API Key");
}
else
{
var identity = new GenericIdentity("API");
identity.AddClaim(new System.Security.Claims.Claim("Origin", "Api"));
var principal = new GenericPrincipal(identity, new string[] {"HermesAdmin"});
// TODO need to think about if I require a JWT Token here.
context.User = principal;
await next();
}
}
else
{
await next();
}
}
else
{
await next();
}
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment