Stardust.Aadb2c.AuthenticationFilter 2.0.1.1

A simple authentication filter for WebApi that supports azure ad b2c

There is a newer version of this package available.
See the version list below for details.
Install-Package Stardust.Aadb2c.AuthenticationFilter -Version 2.0.1.1
dotnet add package Stardust.Aadb2c.AuthenticationFilter --version 2.0.1.1
<PackageReference Include="Stardust.Aadb2c.AuthenticationFilter" Version="2.0.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stardust.Aadb2c.AuthenticationFilter --version 2.0.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

.net Framework

add filter

In WebApiConfig.cs add

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            //   config.SuppressDefaultHostAuthentication();
            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Filters.Add(new OAuthAuthenticationFilter());// turns on aad b2c token validation
            config.Filters.Add(new ErrorFilter());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
Configure filter
In config
<appSettings>
    <!-- mandatory -->
    <add key ="aadTenant" value="tenantName.onmicrosoft.com" />
    <add key="audience" value="you appid" />
    <add key="issuerHostName" value="https://login.microsoftonline.com/{your tenant id}/v2.0/" />
    <!-- optional -->
    <add key ="aadPolicy" value="B2C_1A_SignIn" />
</appSettings>
In code
    protected void Application_Start()
    {
        //Mandatory
        B2CGlobalConfiguration.AadTenant="tenantName.onmicrosoft.com";
        B2CGlobalConfiguration.Audience="you appid";
        B2CGlobalConfiguration.ValidIssuer="https://login.microsoftonline.com/{your tenant id}/v2.0/";
        //Optional
        B2CGlobalConfiguration.AadPolicy="B2C_1A_SignIn"

        //Regular app start stuff
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

asp.net core

add filter
public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddB2CAuthentication("OAuth2", "Azure B2C authentication");//Add the B2C authentication scheme
        }

         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
            app.AddConfigurationManager(new ConfigManager());// Add the configuration binding. Implement your own manager to fit with your configuration scheme.
            //the netcore version uses the same config keys as the .net framework version.
            app.UseMvc();            
        }

.net Framework

add filter

In WebApiConfig.cs add

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            //   config.SuppressDefaultHostAuthentication();
            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Filters.Add(new OAuthAuthenticationFilter());// turns on aad b2c token validation
            config.Filters.Add(new ErrorFilter());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
Configure filter
In config
<appSettings>
    <!-- mandatory -->
    <add key ="aadTenant" value="tenantName.onmicrosoft.com" />
    <add key="audience" value="you appid" />
    <add key="issuerHostName" value="https://login.microsoftonline.com/{your tenant id}/v2.0/" />
    <!-- optional -->
    <add key ="aadPolicy" value="B2C_1A_SignIn" />
</appSettings>
In code
    protected void Application_Start()
    {
        //Mandatory
        B2CGlobalConfiguration.AadTenant="tenantName.onmicrosoft.com";
        B2CGlobalConfiguration.Audience="you appid";
        B2CGlobalConfiguration.ValidIssuer="https://login.microsoftonline.com/{your tenant id}/v2.0/";
        //Optional
        B2CGlobalConfiguration.AadPolicy="B2C_1A_SignIn"

        //Regular app start stuff
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

asp.net core

add filter
public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddB2CAuthentication("OAuth2", "Azure B2C authentication");//Add the B2C authentication scheme
        }

         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
            app.AddConfigurationManager(new ConfigManager());// Add the configuration binding. Implement your own manager to fit with your configuration scheme.
            //the netcore version uses the same config keys as the .net framework version.
            app.UseMvc();            
        }

Release Notes

Updated to support .netcore

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.2.2 70 7/3/2019
2.2.0-rc1 67 5/21/2019
2.0.5 1,149 11/6/2018
2.0.4 438 10/2/2018
2.0.3 598 6/26/2018
2.0.2 252 5/29/2018
2.0.1.2 290 4/4/2018
2.0.1.1 250 4/4/2018
2.0.0 291 3/6/2018
2.0.0-pre0004 218 2/26/2018
2.0.0-pre0003 196 2/26/2018
2.0.0-pre0002 211 2/23/2018
2.0.0-pre0001 216 2/23/2018
1.2.1.5 365 10/23/2017
1.2.1.3 183 10/23/2017
1.2.1.2 200 10/23/2017
1.2.1.1 209 10/19/2017
1.2.1 185 10/19/2017
1.2.0 184 10/17/2017
1.1.5.8 196 10/17/2017
1.1.5.7 378 10/4/2017
1.1.5.6 196 10/4/2017
1.1.5.5 206 10/2/2017
1.1.5.2 265 8/15/2017
1.1.5.1 215 8/15/2017
1.1.5 212 8/15/2017
1.1.4 201 8/15/2017
1.1.3 202 8/15/2017
1.1.2 216 8/15/2017
1.1.1 200 8/7/2017
1.1.0 256 7/11/2017
1.0.0 1,395 7/5/2017
1.0.0-pre2 272 7/5/2017
1.0.0-pre1 267 7/4/2017
Show less