C
C#2mo ago
hutonahill

Capture clients IP address when they call my API

I am making an authentication API. I would like to log the IP address my client as part of my session system. I need a method of capturing the clients IP address that isn't passed in by the client. I've tried HttpContext, but that seems to be passed in by the client who i cant trust.
51 Replies
qqdev
qqdev2mo ago
Are you using ASP.NET?
hutonahill
hutonahill2mo ago
i think so? first c# api ive developed
Pobiega
Pobiega2mo ago
HttpContext.Connection.RemoteIpAddress iirc
qqdev
qqdev2mo ago
Yeah, something like that You can trust that info btw The client shouldn't be able to fake that
Angius
Angius2mo ago
Just be careful about whether the IP is classified as PII in your jurisdiction or not Also, it won't work when using a proxy like Cloudflare, all requests will just have CF's IP You'd have to read the actual IP from X-Forwarded-For header
Buddy
Buddy2mo ago
PII = Personally Identifiable Information
hutonahill
hutonahill2mo ago
i was testing out the API (using the swagger API) and the request header has a place where i could pass in any IP i wanted. Is that jus a swagger thing?
qqdev
qqdev2mo ago
What is the name of that header?
hutonahill
hutonahill2mo ago
request body? or POST?
qqdev
qqdev2mo ago
Those are different things You can send headers as a part of an HTTP request/response POST is an HTTP verb. Other HTTP verbs: GET, POST, PUT, DELETE, PATCH, OPTIONS
hutonahill
hutonahill2mo ago
when I have HttpContext as a paramiter of my login endpoint i see all the elements of the HttpContext here and it allows me to edit them:
No description
qqdev
qqdev2mo ago
No IP tho, right? Wait
Pobiega
Pobiega2mo ago
Uh.. don't put the context as a parameter
hutonahill
hutonahill2mo ago
let me add the HttpContext back and i will show you
qqdev
qqdev2mo ago
You can access it directly It's within the scope
Want results from more Discord servers?
Add your server