Skip to content

Instantly share code, notes, and snippets.

@merken
Last active September 27, 2018 08:07
Show Gist options
  • Save merken/f986a71d55ace17ccd01afcf7f80bcbb to your computer and use it in GitHub Desktop.
Save merken/f986a71d55ace17ccd01afcf7f80bcbb to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace auth.api.Security
{
public static class AuthenticationExtensions
{
public static MvcOptions AddGlobalAzureAuthentication(this MvcOptions mvcOptions)
{
mvcOptions.Filters.Add(AzureAdFilter());
return mvcOptions;
}
private static AuthorizeFilter AzureAdFilter()
{
var policy = new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(Constants.AzureAdScheme)
.RequireAuthenticatedUser()
.Build();
return new AuthorizeFilter(policy);
}
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICustomAuthenticationService, MyDbAuthenticationService>();
services
.AddAzureAdAuthorization(Configuration) // ==> Adds the Policy, custom Bearer Scheme using JWT
.AddMvc(options => options.AddGlobalAzureAuthentication())
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment