Last active
September 27, 2018 08:07
-
-
Save merken/f986a71d55ace17ccd01afcf7f80bcbb to your computer and use it in GitHub Desktop.
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 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); | |
} | |
} | |
} |
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
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