Keycloak.WAPI.Security 1.0.0

dotnet add package Keycloak.WAPI.Security --version 1.0.0
                    
NuGet\Install-Package Keycloak.WAPI.Security -Version 1.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="Keycloak.WAPI.Security" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Keycloak.WAPI.Security" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Keycloak.WAPI.Security" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Keycloak.WAPI.Security --version 1.0.0
                    
#r "nuget: Keycloak.WAPI.Security, 1.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.
#:package Keycloak.WAPI.Security@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Keycloak.WAPI.Security&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Keycloak.WAPI.Security&version=1.0.0
                    
Install as a Cake Tool

Keycloak .NET web API bearer token implementation.

Implementation steps,

  1. Create a Startup class to initialize the keycloak configurations, public class Startup { const string persistentAuthType = "KeycloakAuthentication";

     public void Configuration(IAppBuilder app)
     {
    
         app.UseCookieAuthentication(new CookieAuthenticationOptions
         {
             AuthenticationType = persistentAuthType                
         });
    
         // You may also use this method if you have multiple authentication methods below,
         // or if you just like it better:
         app.SetDefaultSignInAsAuthenticationType(persistentAuthType);
         app.UseKeycloakAuthentication(new KeycloakAuthenticationOptions
         {
    
             KeycloakUrl = "Keycloak server Url",
             Realm = "Realm name",
             ClientId = "XXXXX",
             ClientSecret = "XXXXXXXXXXXXXXXXX",
             VirtualDirectory = "True",
             EnableWebApiMode = true,
             AuthenticationType = persistentAuthType,
             SignInAsAuthenticationType = "True",
             EnableBearerTokenAuth = true,
             //ForceBearerTokenAuth = true,
             //Token validation options - these are all set to defaults
             AllowUnsignedTokens = false,
             DisableIssuerSigningKeyValidation = false,
             DisableIssuerValidation = false,
             DisableAudienceValidation = true
         });            
     }
    

    }

  2. Add the below line of code to the .Net Web API controller class to load the startup class when there is a call to the .Net Web API service. This line of code should be in the namespace reference section. [assembly: OwinStartup(typeof(Startup))]

  3. Add "[Authorize]" attribute either at the API controller level or at the API action level to secure it via keycloak.

Please note that you need to pass the keycloak authentication token as a HTTP header to the calling Web API service . The authorization token would be get on valid login by the user. Also, remember to set the HTTP header for the token as "Authorization" header to read by the Keycloak.WAPI.Security otherwise fails.

Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  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.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 811 8/9/2019