Stardust.Aadb2c.AuthenticationFilter 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Stardust.Aadb2c.AuthenticationFilter --version 2.0.0                
NuGet\Install-Package Stardust.Aadb2c.AuthenticationFilter -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: Stardust.Aadb2c.AuthenticationFilter, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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                

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Framework net471 is compatible.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.5.1 520 4/9/2024
2.5.0 3,947 3/5/2023
2.4.0 2,814 1/4/2023 2.4.0 is deprecated because it has critical bugs.
2.3.3 2,515 7/8/2022 2.3.3 is deprecated because it has critical bugs.
2.3.2 869 7/8/2022 2.3.2 is deprecated because it has critical bugs.
2.2.3 10,185 10/7/2019 2.2.3 is deprecated because it has critical bugs.
2.2.2 2,467 7/3/2019 2.2.2 is deprecated because it has critical bugs.
2.2.0-rc1 892 5/21/2019
2.0.5 2,739 11/6/2018
2.0.4 2,488 10/2/2018
2.0.3 1,743 6/26/2018
2.0.2 1,465 5/29/2018
2.0.1.2 1,378 4/4/2018
2.0.1.1 1,396 4/4/2018
2.0.0 1,458 3/6/2018
2.0.0-pre0004 1,164 2/26/2018
2.0.0-pre0003 1,092 2/26/2018
2.0.0-pre0002 1,357 2/23/2018
2.0.0-pre0001 1,272 2/23/2018
1.2.1.5 1,334 10/23/2017
1.2.1.3 1,194 10/23/2017
1.2.1.2 1,268 10/23/2017
1.2.1.1 1,170 10/19/2017
1.2.1 1,197 10/19/2017
1.2.0 1,237 10/17/2017
1.1.5.8 1,287 10/17/2017
1.1.5.7 1,427 10/4/2017
1.1.5.6 1,287 10/4/2017
1.1.5.5 1,232 10/2/2017
1.1.5.2 1,296 8/15/2017
1.1.5.1 1,287 8/15/2017
1.1.5 1,205 8/15/2017
1.1.4 1,209 8/15/2017
1.1.3 1,252 8/15/2017
1.1.2 1,264 8/15/2017
1.1.1 1,124 8/7/2017
1.1.0 1,307 7/11/2017
1.0.0 3,848 7/5/2017
1.0.0-pre2 1,215 7/5/2017
1.0.0-pre1 1,263 7/4/2017

Updated to support .netcore