An warware: net core sami m ip

Tabbas, bari mu fara da magana game da NET Core da matsalar samun adireshin IP mai nisa.

NET Core kyauta ce, dandamalin giciye, dandamali mai haɓaka tushen tushen tushe don gina nau'ikan aikace-aikace da yawa, gami da aikace-aikacen yanar gizo da sabis. Koyaya, samun adireshin IP mai nisa a cikin aikace-aikacen NET Core na iya haifar da ƙalubale a wasu lokuta. Wannan yana da mahimmanci saboda samun damar yin amfani da adireshin IP na abokin ciniki yana ba da damar haɓakar shiga, abubuwan da suka dace, da ƙari.

Don magance wannan, za mu iya amfani da HttpContext wanda ginannen ɗakin karatu na NET Core ya samar.

Aiwatar da Magani

Mun fara da samun dama ga HttpContext a cikin hanyar sarrafawa inda muke buƙatar IP na abokin ciniki. The HttpContext abu ya ƙunshi takamaiman bayanin HTTP game da buƙatun HTTP guda ɗaya.

Fihirisar Sakamakon Sakamakon Jama'a ()
{
var remoteIpAddress = HttpContext.Connection.RemoteIpAddress;
// ƙarin dabaru…
}

Wannan yana samun ku abokin ciniki IP address. Hanyar HttpContext.Connection.RemoteIpAddress tana mayar da adireshin IP na abokin ciniki mai nisa. Koyaya, lura cewa wannan ba koyaushe zai dawo da sakamakon da ake tsammani ba, musamman lokacin da uwar garken ku ke bayan ma'aunin nauyi ko amfani da tsakiyar kayan kai na gaba.

Zurfafa fahimtar Code

Lambar C # da aka bayar tana amfani da HttpContext abu, wanda mallakar ControllerBase ne kuma ya ƙunshi bayani game da buƙatar HTTP. Wannan ya haɗa da abubuwa kamar bayanan mai masaukin baki, cikakkun bayanan hanya, da duk mahimman kayan Haɗin haɗi.

Kayan Haɗin na nau'in ConnectionInfo ne, daga Microsoft.AspNetCore.Http.Features sararin suna. Daga cikin wasu kaddarorin, yana ƙunshe da `RemoteIpAddress`, wata kadara wacce ke debo adireshin IP daga buƙatun HTTP.

Koyaya, uwar garken na iya kasancewa a bayan ma'aunin nauyi ko wakili. Waɗannan masu shiga tsakani na iya canza ainihin bayanin buƙatar HTTP, gami da tushen IP address. Lokacin da wannan ya faru, kayan 'RemoteIpAddress' suna ba da adireshin IP na na'ura na tsaka-tsaki, ba na ainihin abokin ciniki ba.

Maganin 'Forwarded Headers Middleware'

Don aikace-aikacen da ke gudana a bayan irin waɗannan masu shiga tsakani, NET Core yana ba da 'ForwardedHeadersMiddleware', matsakaiciyar kayan aiki tare da masu kai 'X-Forwarded-*' don riƙe ainihin bayanan buƙatar HTTP.

Don kunna 'ForwardedHeadersMiddleware', ƙara saitin mai zuwa zuwa aji na Startup.cs.

Tsare-tsaren banza na jama'a (IApplicationBuilder app, IWebHostEnvironment env)
{
app.Amfani da Masu kai da kai(sabbin Zaɓuɓɓukan Masu Gabatarwa
{
ForwardedHeaders = Masu Gabatarwa.XForwardedFor |
Masu Gabatarwa.XForwardedProto
});

//…
}

Ƙaddamar da HttpContext da Masu kai Gaba

Kuna iya ci gaba da amfani da HttpContext.Connection.RemoteIpAddress kamar da, amma yanzu, idan akwai, zai mayar da adireshin IP na abokin ciniki daidai, koda kuwa uwar garken yana bayan wakili ko ma'aunin nauyi.

Ka tuna cewa a cikin hadadden filin shirye-shiryen yanar gizo, middleware da kuma daidai amfani da mahallin abubuwa kamar HttpContext na iya yin aiki da alama mai wahala kamar samun IP mai nisa mai sauƙi. Don dawo da adireshin IP na abokin ciniki, muna buƙatar fahimtar ƙaƙƙarfan ƙa'idar HTTP, rawar tsakiya kamar 'ForwardedHeadersMiddleware', da kuma amfanin HttpContext abu.

Shafi posts:

Leave a Comment