twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
525 stars 83 forks source link

check for token before proxying request #65

Closed Steffen-MLR closed 4 years ago

Steffen-MLR commented 4 years ago

Hi,

I want to check a token before proxying a request. I have this:

[Route("forwarded-server/api/{targetEndpoint}")]
public Task CheckTokenAndProxy(string targetEndpoint)
{
    if (Request.Headers.TryGetValue("token", out StringValues token))
    {
       if (Validate(token)) return this.HttpProxyAsync($"https://forwarded-server/api/{targetEndpoint}");
    }
    return ?;
}

As you can see the questionmark, i dont know how to solve this. I tried to do this.HttpProxyAsync("https://myserver/forwarded-server/forbidden") and implement another route `with an IActionResult and a Forbid(), but this gives an SSL Error (The Certificate is Self-signed).

Is there either a way to solve this like I did by disabling ssl check for the proxy target or am I doing this completey wrong?

twitchax commented 4 years ago

I would just do something like.

this.Response.StatusCode = 403;
// Write to headers or body if you want here.
return Task.CompletedTask;
Steffen-MLR commented 4 years ago

That is exactly what I needed, thank you!