GithubHelp home page GithubHelp logo

jonathanvil / kinde-authentication Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clinically-au/kinde-authentication

0.0 0.0 0.0 413 KB

Library to simplify integration of Kinde (www.kinde.com) with .NET8 apps

License: GNU General Public License v3.0

Shell 0.05% C# 99.95%

kinde-authentication's Introduction

Integrating Kinde Auth with .NET8 Apps (including Blazor)

This library assists with integrating Kinde with .NET applications. It is still in development but usable. There may be breaking changes from version to version for now.

Add the following NuGet package: Clinically.Kinde.Authentication

The following needs to be in your appSettings.json on the server:

{
  "Kinde": {
    "Domain": "<From Kinde>",
    "ClientId": "<From Kinde>",
    "ClientSecret": "<From Kinde>",
    "ManagementApiAudience": "<From Kinde>", // Optional - only need to set this if using custom domains
    "SignedOutRedirectUri": "https://localhost:5001/signout-callback-oidc",
    "JwtAudience": "<From Kinde - Audience for API, if using JWT Bearer Auth in addition to Identity>"
  },
  "AppConfig": {
    "BaseUrl": "https://localhost:5001"
  }
}

Remember to give your app access to the Kinde Management API!

You can omit JwtAudience if you are not using JWT Bearer Authentication.

If you want users to log in to your MVC / Razor Page / Blazor app, you need to add this to your Program.cs:

builder.Services.AddKindeIdentityAuthentication(opt =>
{
    opt.UseMemoryCacheTicketStore = false; // optional - default to false
}); 

If you want to add JwtBearer Authentication to your API, add this to your Program.cs (remember to add the JwtAudience to your appSettings.json):

builder.Services.AddKindeJwtBearerAuthentication();

Then add the standard authorization services:

builder.Services.AddAuthorization();

And:

app.MapKindeIdentityEndpoints();

For Blazor WASM, you also need to add this to Program.cs on the client:

builder.Services.AddKindeWebAssemblyClient();

Roles

You can use the standard Authorize attribute:

[Authorize(Roles = "Admin")]

Permissions

In order to add authorization policies for your Kinde permissions:

builder.Services
    .AddAuthorizationBuilder()
    .AddKindePermissionPolicies<Permissions>();

Then create a Permissions class that contains all the Kinde permissions you want to use:

public class Permissions
{
    public const string MyPermissionName = "myPermissionNameInKinde";
}

Then you can use the permissions in your controllers or Razor pages:

[Authorize(Policy = Permissions.MyPermissionName)]

Notes

  • You need to go to the Tokens section of your app, and enable the Roles and Email claims in the access token.
  • In order to access the management API (e.g. to add users programmatially etc), inject KindeManagementClient into your services. Note you will need a separate M2M app in Kinde for this, with access to the Management API.
  • You can also inject KindeUserManager instead of the standard UserManager to get access to Kinde-specific methods.
  • Inject BlazorUserAccessor to get access to the current user in your Blazor components.

I've only recently worked out how to tie all this together, so some bits may not be entirely required etc. Raise an issue if you notice any problems.

Example Projects

To Do List:

  • Feature flags not currently implemented (but will work the same way as Permissions)
  • Support more claims/properties in the strongly typed user objects

kinde-authentication's People

Contributors

clinically-au avatar jonathanvil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.