1

I'm trying to create a .net core 2.2 web api with custom authentication scheme but I'm not able to read the content of my http request containing an authorization parameter. I've created the following attribute:

    [AttributeUsage(validOn: AttributeTargets.Class | AttributeTargets.Method)]
public class ApiKeyAuthAttribute : Attribute, IAsyncActionFilter
{

    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {

        var auth = context.HttpContext.Request.Headers["Authorization"];

        await next();
    }
}

Then I decorated the calls with the "[ApiKeyAuthAttribute]" The attribute is called but "auth" is always empty. I've inspected the http call and it contains the following string:

GET http://localhost:5000/secret HTTP/1.1
Authorization: hmacauth 65d3a4f0-0239-404c-8394-21b94ff50604:YasPG+z7r1jyUUqAlXY9G91Ov0IfDfA9sNvW4NLocIU=:4977ca7250414e9c8c8b3d9a703fcf9e:1596303518
Host: localhost:5000

I guess that I've to put something in the application startup but I'm not sure..

This is my current application startup

        // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        
        app.UseHttpsRedirection();

        app.UseMvc();
    }
1
  • I also tried to add Authentication / Authorization to app and services in the startup file but nothing changed.. Commented Aug 4, 2020 at 9:06

1 Answer 1

2

I was finally able to get the Authorization header using this function:

context.HttpContext.Request.Headers.TryGetValue("Authorization", out StringValues authString);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.