CodeWorks.Auth0Provider 1.1.0

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

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

CodeWorks.Auth0Provider

dotnet add package CodeWorks.Auth0Provider

Configure UserInfo (required)

In Auth0 you can create 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
 */
function extendUserInfo(user, context, callback) {
    context.idToken = context.idToken || {};
    context.authorization = context.authorization || {};
    user.app_metadata = user.app_metadata || { };
    user.app_metadata.new = user.app_metadata.id ? false : true;
    user.app_metadata.id = user.app_metadata.id || generateId();

    for (const key in user.app_metadata) {
        context.idToken[key] = user.app_metadata[key];
    }
    context.idToken['roles'] = context.authorization.roles;
    context.idToken['permissions'] = context.authorization.permissions;
    context.idToken['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);
        });
        
    function generateId() {
      let timestamp = (new Date().getTime() / 1000 | 0).toString(16);
      return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, () => (
        Math.random() * 16 | 0).toString(16)).toLowerCase();
    }
}

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 ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<CodeWorks.Utils.Auth0Provider>();
}

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

AccountController.cs

public class AccountController : ControllerBase
  {
    // Uses Dependency IOC 
    private readonly Auth0Provider _auth0Provider;

    public AccountController(Auth0Provider auth0Provider)
    {
      _auth0Provider = auth0Provider;
    }

    [HttpGet]
    [Authorize] // Works with native controls
    public async Task<ActionResult<Account>> Get()
    {
      try
      {
        // retrieves userInfo from Auth0 or CacheControl
        Account userInfo = await _auth0Provider.GetUserInfoAsync<Account>(HttpContext);
        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,026 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