Skip to content

Instantly share code, notes, and snippets.

@ahelland
Last active December 11, 2018 07:30
Show Gist options
  • Save ahelland/276b9edd80ca7265065aaffc50bb79cf to your computer and use it in GitHub Desktop.
Save ahelland/276b9edd80ca7265065aaffc50bb79cf to your computer and use it in GitHub Desktop.
Startup file for bootstrapping client certificates / signed JWTs from ADFS (or Azure AD)
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.MetadataAddress = "https://adfs.contoso.com/adfs/.well-known/openid-configuration";
options.Validate();
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidIssuer = "http://adfs.contoso.com/adfs/services/trust",
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
ValidAudience = "https://contoso.com/api",
RequireSignedTokens = true,
ValidateActor = true,
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("Certificate", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c =>
(c.Type == System.Security.Claims.ClaimTypes.AuthenticationMethod &&
c.Value == "http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/tlsclient" ||
c.Value == "http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/x509" ))));
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment