Stardust.Aadb2c.AuthenticationFilter 2.0.0

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.0
dotnet add package Stardust.Aadb2c.AuthenticationFilter --version 2.0.0
<PackageReference Include="Stardust.Aadb2c.AuthenticationFilter" Version="2.0.0" />
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.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Stardust.Aadb2c.AuthenticationFilter

Simple Authentication Filter for WebApi that supports Azure AD B2C

Usage

Install nuget package

PM>  Install-Package Stardust.Aadb2c.AuthenticationFilter -Version 2.0.0-pre0004

.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();            
        }

Swagger UI support for OAuth2 implicit grant flow

Usage

Install nuget package

PM>  Install-Package Swashbuckle
PM>  Install-Package Stardust.Aadb2c.Swagger

note: Install the Swashbuckle package first, this ensures that the swaggerconfig is crated properly

enable oauth support

In App_Start/SwaggerConfig.cs add the following

    GlobalConfiguration.Configuration
        .EnableSwagger(c =>
            {
                c.EnableAzureAdB2cOAuth2(
                                            tenantId, true,
                                            new ScopeDescription
                                            {
                                                Description = "Allow the service to act on behalf of the user",
                                                ScopeName =  scopeName //usually in the format: https://tenantName.onmicrosoft.com/appId/scopeName (https://stardustfx123.onmicrosoft.com/739B91C4-26A7-4D6C-9344-5FF77A87C09A/user_impersonation)
                                            });
            }).EnableSwaggerUi(c =>
                {
                    c.EnableAzureAdB2cOAuth2(swaggerUiClientId, "B2C_1A_SignIn");
                    
                }); 
alternative

you can keep all the parameters passed to the swagger in the config file.

    GlobalConfiguration.Configuration
        .EnableSwagger(c =>
            {
                c.EnableAzureAdB2cOAuth2();
            }).EnableSwaggerUi(c =>
                {
                    c.EnableAzureAdB2cOAuth2();
                    
                }); 
<appSettings>
    <!-- mandatory -->
    <add key ="aadTenantId" value="tenantId" /><!-- this is a guid -->
    <!-- scopes are separated by | and name and description is separated by ; -->
    <add key="aadScopes" value="email;send email|https://stardustfx123.onmicrosoft.com/739B91C4-26A7-4D6C-9344-5FF77A87C09A/user_impersonation;Allow the service to act on behalf of the user" />
    <add key="aadFlowDescription" value="OAuth2 Implicit Grant" />
    <add key ="aadPolicy" value="B2C_1A_SignIn" />
    <add key ="aadUseV2Endpoint" value="true" />
    <add key ="swaggerClientId" value="swaggerAppId" /><!-- this is a guid -->
    <add key ="swaggerClientSecret" value="secret" /><!-- not recommended to use this -->
    <add key ="swaggerAppName" value="Swagger UI" />
</appSettings>

Stardust.Aadb2c.AuthenticationFilter

Simple Authentication Filter for WebApi that supports Azure AD B2C

Usage

Install nuget package

PM>  Install-Package Stardust.Aadb2c.AuthenticationFilter -Version 2.0.0-pre0004

.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();            
        }

Swagger UI support for OAuth2 implicit grant flow

Usage

Install nuget package

PM>  Install-Package Swashbuckle
PM>  Install-Package Stardust.Aadb2c.Swagger

note: Install the Swashbuckle package first, this ensures that the swaggerconfig is crated properly

enable oauth support

In App_Start/SwaggerConfig.cs add the following

    GlobalConfiguration.Configuration
        .EnableSwagger(c =>
            {
                c.EnableAzureAdB2cOAuth2(
                                            tenantId, true,
                                            new ScopeDescription
                                            {
                                                Description = "Allow the service to act on behalf of the user",
                                                ScopeName =  scopeName //usually in the format: https://tenantName.onmicrosoft.com/appId/scopeName (https://stardustfx123.onmicrosoft.com/739B91C4-26A7-4D6C-9344-5FF77A87C09A/user_impersonation)
                                            });
            }).EnableSwaggerUi(c =>
                {
                    c.EnableAzureAdB2cOAuth2(swaggerUiClientId, "B2C_1A_SignIn");
                    
                }); 
alternative

you can keep all the parameters passed to the swagger in the config file.

    GlobalConfiguration.Configuration
        .EnableSwagger(c =>
            {
                c.EnableAzureAdB2cOAuth2();
            }).EnableSwaggerUi(c =>
                {
                    c.EnableAzureAdB2cOAuth2();
                    
                }); 
<appSettings>
    <!-- mandatory -->
    <add key ="aadTenantId" value="tenantId" /><!-- this is a guid -->
    <!-- scopes are separated by | and name and description is separated by ; -->
    <add key="aadScopes" value="email;send email|https://stardustfx123.onmicrosoft.com/739B91C4-26A7-4D6C-9344-5FF77A87C09A/user_impersonation;Allow the service to act on behalf of the user" />
    <add key="aadFlowDescription" value="OAuth2 Implicit Grant" />
    <add key ="aadPolicy" value="B2C_1A_SignIn" />
    <add key ="aadUseV2Endpoint" value="true" />
    <add key ="swaggerClientId" value="swaggerAppId" /><!-- this is a guid -->
    <add key ="swaggerClientSecret" value="secret" /><!-- not recommended to use this -->
    <add key ="swaggerAppName" value="Swagger UI" />
</appSettings>

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 69 7/3/2019
2.2.0-rc1 67 5/21/2019
2.0.5 1,140 11/6/2018
2.0.4 438 10/2/2018
2.0.3 596 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 180 10/17/2017
1.1.5.8 192 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,393 7/5/2017
1.0.0-pre2 272 7/5/2017
1.0.0-pre1 267 7/4/2017
Show less