Stardust.Aadb2c.AuthenticationFilter 2.0.0

.NET Core 2.0 .NET Framework 4.7.1
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.
#r "nuget: Stardust.Aadb2c.AuthenticationFilter, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Stardust.Aadb2c.AuthenticationFilter as a Cake Addin
#addin nuget:?package=Stardust.Aadb2c.AuthenticationFilter&version=2.0.0

// Install Stardust.Aadb2c.AuthenticationFilter as a Cake Tool
#tool nuget:?package=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>
    
    <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/" />
    
    <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>
    
    <add key ="aadTenantId" value="tenantId" />
    
    <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" />
    <add key ="swaggerClientSecret" value="secret" />
    <add key ="swaggerAppName" value="Swagger UI" />
</appSettings>

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Framework net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Stardust.Aadb2c.AuthenticationFilter:

Package Downloads
Stardust.Aadb2c.Swagger

Helper extensions to configure Swashbuckle Swagger and Swagger UI to use OAuth implicit grant with Azure AD b2c

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.3 96 7/8/2022
2.3.2 61 7/8/2022
2.2.3 7,253 10/7/2019
2.2.2 1,793 7/3/2019
2.2.0-rc1 381 5/21/2019
2.0.5 2,153 11/6/2018
2.0.4 1,617 10/2/2018
2.0.3 1,126 6/26/2018
2.0.2 794 5/29/2018
2.0.1.2 771 4/4/2018
2.0.1.1 761 4/4/2018
2.0.0 781 3/6/2018
2.0.0-pre0004 666 2/26/2018
2.0.0-pre0003 661 2/26/2018
2.0.0-pre0002 658 2/23/2018
2.0.0-pre0001 654 2/23/2018
1.2.1.5 797 10/23/2017
1.2.1.3 648 10/23/2017
1.2.1.2 649 10/23/2017
1.2.1.1 649 10/19/2017
1.2.1 639 10/19/2017
1.2.0 644 10/17/2017
1.1.5.8 642 10/17/2017
1.1.5.7 816 10/4/2017
1.1.5.6 650 10/4/2017
1.1.5.5 644 10/2/2017
1.1.5.2 722 8/15/2017
1.1.5.1 676 8/15/2017
1.1.5 658 8/15/2017
1.1.4 675 8/15/2017
1.1.3 659 8/15/2017
1.1.2 683 8/15/2017
1.1.1 645 8/7/2017
1.1.0 709 7/11/2017
1.0.0 2,757 7/5/2017
1.0.0-pre2 732 7/5/2017
1.0.0-pre1 714 7/4/2017

Updated to support .netcore