Coder Social home page Coder Social logo

aspnetcore.security.auth0's Introduction

AspNetCore.Security.Auth0

Asp Net Core Jwt Bearer Token Security package for integration with Auth0.

Info

NuGet NuGet Badge

Auth0 settings

You need two applications :

  1. Single Page Application: used to authenticate the user in a SPA
  2. Machine to Machine: used by this project to read users + profile (AppData)

See the wiki : HowTo for more details.

C# Project changes

appsettings.json

"Auth0Options": {
    "JwtAuthority": "https://abc.eu.auth0.com/",
    "JwtAudience": "https://my-test-api",
    "Policies": [ "read:data" ],

    "Audience": "https://abc.eu.auth0.com/api/v2/",
    "ClientId": "<replace>",
    "ClientSecret": "<replace>",
    "Domain": "https://abc.eu.auth0.com/"
}

Startup.cs

ConfigureServices()
public void ConfigureServices(IServiceCollection services)
{
    // Configure MVC AuthorizeFilter
    services.AddMvc(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    // other services ...

    // Add Auth0
    services.AddAuth0(options =>
    {
        var section = Configuration.GetSection("Auth0Options");

        options.JwtAuthority = section["JwtAuthority"]; // Something like https://abc.eu.auth0.com/
        options.JwtAudience = section["JwtAudience"]; // The API Identfier

        options.Audience = section["Audience"]; // Something like https://abc.eu.auth0.com/api/v2/
        options.ClientId = section["ClientId"]; // The Client ID from the Machine 2 Machine
        options.ClientSecret = section["ClientSecret"]; // The Client Secret from the Machine 2 Machine
        options.Domain = section["Domain"]; // Something like https://abc.eu.auth0.com/
        options.Policies = section.GetSection("Policies").Get<List<string>>(); The policies, like "read:data" and "write:data"
    });
}
Configure()
public void Configure(...)
{
    // Add the authentication middleware to the middleware pipeline
    app.UseAuthentication();

    app.UseMvc();
}
Controllers

Update your controllers to add Authorize and use the correct policy:

namespace Sample.Frontend.Controllers
{
    [Authorize("read:data")]
    [ApiController]
    public class DataQueryController : ControllerBase
    {
        // more code ...
    }
}

aspnetcore.security.auth0's People

Contributors

dependabot[bot] avatar stefh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

aspnetcore.security.auth0's Issues

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.