GithubHelp home page GithubHelp logo

boeschenstein / webapi_restclient_bearertoken Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 28 KB

Authorization, jwt: create bearer token in REST client to access ASP.NET Core Web Api. IHttpClientFactory, HttpClient

C# 100.00%
aspnet-web-api httpclient httpclientfactory ihttpclientfactory polly resiliency resilient

webapi_restclient_bearertoken's Introduction

WebAPI + REST client + Bearer token

Access Token in Controller

string scheme = "Bearer";
string result1 = await HttpContext.GetTokenAsync(scheme, "access_token"); // works, but it is just the token as string
string result2 = await HttpContext.GetTokenAsync("access_token"); // works, but it is just the token as string

// AuthenticateResult result3a = await HttpContext.AuthenticateAsync(); // works also
AuthenticateResult result = await HttpContext.AuthenticateAsync(scheme);
ClaimsPrincipal principal = result.Principal;
IEnumerable<Claim> claims = principal.Claims;
foreach (var item in claims)
{
    // Console.WriteLine($"Role: {item.Type}:{item.Value}");
    if (item.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" || item.Type == "role")
        Console.WriteLine($"Role: {item.Value}");
    if (item.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" || item.Type == "sub")
        Console.WriteLine($"Subject: {item.Value}");
}

Access Token in StartUp (rarely used)

OnTokenValidated = contex =>
{
    Console.WriteLine($"---- OnTokenValidated Start ----");
    Console.WriteLine($"Issuer: {contex.SecurityToken.Issuer}");
    JwtSecurityToken jwt = (JwtSecurityToken)contex.SecurityToken;
    foreach (Claim claim in jwt.Claims)
    {
        //Console.WriteLine($"Claim: {claim.Type}={claim.Value}");
        if (claim.Type == "role")
        {
            Console.WriteLine($"Role:{claim.Value}");
        }
    }
    Console.WriteLine($"Subject: {jwt.Subject}");
    foreach (string audience in jwt.Audiences)
    {
        Console.WriteLine($"Audience:{audience}");
    }
    Console.WriteLine($"---- OnTokenValidated End ----");

    return Task.CompletedTask;
},

Optional, rarely needed

CustomAuthorizationHandler

Register handler

public void ConfigureServices(IServiceCollection services)
{
    ...

    // CustomAuthorizationHandler instead of Authorize attribute
    services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();

    services.AddControllers();
}

Handler ()

    public class CustomAuthorizationHandler : IAuthorizationHandler
    {
        public Task HandleAsync(AuthorizationHandlerContext context)
        {
            //var authFilterCtx = (Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext)context.Resource; // todo: cast not working...
            //string authHeader = authFilterCtx.HttpContext.Request.Headers["Authorization"];
            //if (authHeader != null && authHeader.Contains("Bearer"))
            //{
            //    var token = authHeader.Replace("Bearer", "");
            //    // Now token can be used for further authorization
            //}

            // throw new NotImplementedException();
            return Task.CompletedTask;
        }
    }

HttpClient

Typed Client

Typed Client: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-7.0#typed-clients

// How to register HttpClient using a Service and Interface: https://stackoverflow.com/questions/56091875/ihttpclientfactory-in-asp-net-core-typed-clients
services.AddHttpClient<ICheckPermissionClient, CheckPermissionClient>(c =>
{
    c.BaseAddress = new Uri("https://i-pfw.corproot.net");
    c.DefaultRequestHeaders.Add("Accept", "application/json");
    c.DefaultRequestHeaders.Add("User-Agent", "SMMS-PFW-Client");
});
services.AddTransient<ICheckPermission, CheckPermission>();

Generate API using Swagger and NSwag

  • Get swagger file from REST Api
  • Install NSWag UI
  • Generate Client, using Swagger File
    • name: CheckPermissionClient
    • generate an interface: ICheckPermissionClient
  • Register HttpClient (code above)
  • Create a service:
public class CheckPermission : ICheckPermission
{
    private readonly ICheckPermissionClient client;

    public CheckPermission(ICheckPermissionClient client)
    {
        this.client = client;
    }
    public async Task<bool> HavePermissionAsync(string myArguments)
    {
        var ret = await this.client.CheckPermissionsAsync(myArguments);
        return ret;
    }
}

Information

webapi_restclient_bearertoken's People

Contributors

boeschenstein avatar

Watchers

 avatar  avatar

Forkers

toan2604

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.