CodeWorks.Auth0Provider 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeWorks.Auth0Provider --version 1.0.1                
NuGet\Install-Package CodeWorks.Auth0Provider -Version 1.0.1                
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="CodeWorks.Auth0Provider" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeWorks.Auth0Provider --version 1.0.1                
#r "nuget: CodeWorks.Auth0Provider, 1.0.1"                
#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 CodeWorks.Auth0Provider as a Cake Addin
#addin nuget:?package=CodeWorks.Auth0Provider&version=1.0.1

// Install CodeWorks.Auth0Provider as a Cake Tool
#tool nuget:?package=CodeWorks.Auth0Provider&version=1.0.1                

CodeWorks.Auth0Provider

dotnet add package CodeWorks.Auth0Provider

Configure UserInfo (required)

In Auth0 you can create namespaced properties directly onto your userInfo Token. This is best accomplished with auth rules. In your Auth0 Dashboard be sure to enable RBAC and add in this custom rule

//AUTH0 RULE
/**
 * Add common namespaced properties to userInfo, 
 * note auth0 will strip any non namespaced properties
 */
function extendUserInfo(user, context, callback) {
    const uuid = require('uuid@3.3.2');
    const namespace = 'https://YOURDOMAINHERE.auth0.com';
    context.idToken = context.idToken || {};
    context.authorization = context.authorization || {};
    user.app_metadata = user.app_metadata || { new: true };
    user.app_metadata.id = user.app_metadata.id || uuid();

    // Enabled to map app_metadata properties to top level Profile
    for (const key in user.app_metadata) {
        context.idToken[`${namespace}/${key}`] = user.app_metadata[key];
    }
    context.idToken[`${namespace}/roles`] = context.authorization.roles;
    context.idToken[`${namespace}/permissions`] = context.authorization.permissions;
    context.idToken[`${namespace}/user_metadata`] = user.user_metadata;
    
    if(!user.app_metadata.new){
        return callback(null, user, context);
    }
    delete user.app_metadata.new;
    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
        .then(function () {
            callback(null, user, context);
        })
        .catch(function (err) {
            callback(err);
        });
}

ConfigureKeyMap

Properties that are added to Auth0 Tokens via rules can be retrieved with the following configuration. Namespaced properties are mapped to the top level class.

Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...
    Auth0ProviderExtension.ConfigureKeyMap(new List<string>() { "id", "roles", "permissions" });
    // ...
}

UserInfoModel

Create you own UserInfo model and have the library extract and cast to your prefered class with generics.

Profile.cs

  public class Profile
  {
    public string Id { get; set; }  // Will be mapped to the id (uuid) from Auth0
    public string Name { get; set; }
    public string Email { get; set; }
    public string Picture { get; set; }
    public List<string> Roles { get; set; } = new List<string>(); // Will be mapped to the Roles from Auth0
    public List<string> Permissions { get; set; } = new List<string>(); // Will be mapped to the Permissions from Auth0
  }

HttpContext

extract the UserInfo in your controllers via HttpContext

SomeController.cs

[HttpGet]
[Authorize]
public async Task<ActionResult<Profile>> GetProfile(string id)
{
    try
    {
        Profile userInfo = await HttpContext.GetUserInfoAsync<Profile>();
        return Ok(userInfo)
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}
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 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 1,349 10/16/2023
1.1.0 2,030 10/3/2022
1.0.3 1,288 10/22/2020
1.0.2 400 10/22/2020
1.0.1 422 10/22/2020
1.0.0 535 10/17/2020