Asp.Net Core RC1, OpenIdConnect, JWT and Angular 2 SPA - Part 1

Working with Asp.Net Core and Angular 2 at the time of writing may feel like a trail blazing experience, especially given the lack of documentation and stability in the underling frameworks, libraries and tools, leading to lost time in debugging and searching for answers.

In the hope of documenting some of my own recent experiences integrating these technologies and Microsoft’s micro-services framework Service Fabric, I’ll dive into specific code areas which have proven fiddley. To start off I should preface that the version of Asp.Net Core I’m currently targeting is RC1 and some bugs and workarounds will not apply to subsequent framework versions. Moreover the Service Fabric version targeted is Service Fabric SDK (version 2.0.217) and Service Fabric Runtime (version 5.0.217).

To begin, we’ll start by configuring CookieAuthentication, OpenIdConnectAuthentication, JwtBearerAuthentication, Mvc & SPA routes in our Asp.Net Core Web project Startup.cs file.

Note that the below code supports multi-tenant Azure AD authentication and is meant for development scenarios as ValidateIssuer and RequireHttpsMetadata are both set to false for simplicity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}

app.UseIISPlatformHandler();
app.UseStaticFiles();

app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.CookieSecure = CookieSecureOption.Never;
// The default setting for cookie expiration is 14 days. SlidingExpiration is set to true by default
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
});

var acmeOptions = app.ApplicationServices.GetService<IOptions<AcmeOptions>>().Value;

app.UseOpenIdConnectAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.ClientId = acmeOptions.AzureAd.ClientId;
options.Authority = AcmeConstants.AuthEndpointPrefix + "common/";
options.PostLogoutRedirectUri = acmeOptions.AzureAd.PostLogoutRedirectUri;
options.CallbackPath = AcmeRouteConstants.LoginCallbackRoute;
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false };
options.RequireHttpsMetadata = false;
options.Events = new OpenIdConnectAuthenticationEvents(acmeOptions.AzureAd)
{
OnAuthenticationFailed = context => OpenIdConnectAuthenticationEvents.GetFailedResponse(context)
};
});

app.UseJwtBearerAuthentication(options =>
{
options.AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme;
options.Audience = acmeOptions.AzureAd.JwtAudience;
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.Authority = AcmeConstants.Security.AuthEndpointPrefix + "common/";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
};
options.RequireHttpsMetadata = false;
options.Events = new JwtBearerAuthenticationEvents
{
OnAuthenticationFailed = context => JwtBearerAuthenticationEvents.GetFailedResponse(context)
};
});

app.UseMvc(routes =>
{
routes.MapRoute(
name: "webapi",
template: "api/{controller}/{action}/{id?}");

routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
});
}

Apart from both Jwt and OpenIdConnect support, of interest is the implementation of custom event overrides via the OpenIdConnectAuthenticationEvents and JwtBearerAuthenticationEvents classes. As OnAuthenticationFailed cannot be overridden within the derived event classes we wire up our custom logic as below:

OnAuthenticationFailed = context => OpenIdConnectAuthenticationEvents.GetFailedResponse(context)

OnAuthenticationFailed = context => JwtBearerAuthenticationEvents.GetFailedResponse(context)

In Part 2 we’ll upgrade our code to Asp.Net Core RC2 and add support for Swagger and AutoRest.