日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Asp.Net Core 中如何设置 IP 白名单

發(fā)布時間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Asp.Net Core 中如何设置 IP 白名单 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

咨詢區(qū)

  • MartinM

我想在一個web站點中實現(xiàn)ip白名單功能,在 MVC 時代我只需要在 web.config 中添加如下配置即可。

<security><ipSecurity?allowUnlisted="false"?denyAction="NotFound"><add?allowed="true"?ipAddress="XX.XX.XX.XX"?subnetMask="255.255.255.0"/></ipSecurity> </security>

但在 AspNetCore 中使用如上配置時,程序啟動就會報錯。

Unable?to?start?process?The?web?server?request?failed?with?status?code?500,?internal?server?error

很顯然這個 config 配置是有問題的,請問是否有內(nèi)置或者第三方的組件來實現(xiàn)這個功能?

回答區(qū)

  • Ergwun

Damian Bod 寫了一篇文章:https://damienbod.com/2016/12/18/implementing-a-client-white-list-using-asp-net-core-middleware/ 來演示如果通過中間件的方式來實現(xiàn) ip 白名單功能。

他分別給了一個 全局性中間件 和 actionfilter 兩種方式來實現(xiàn),無論哪種方式都需要在 appsetttings.json 中配置可允許訪問的 ip 列表,然后根據(jù)這個list來檢查客戶端的ip是否符合?client ip 可以通過 context.Connection.RemoteIpAddress 來獲取。

如果你有更精細(xì)化的配置,比如說設(shè)置ip段,那你可以通過nuget上的 IPAddressRange 包來實現(xiàn),它支持了多種格式,如:192.168.0.0/24 和 192.168.0.0/255.255.255.0,包括 CIDR 表達(dá)式和 IPv6。

參考下面的例子。

  • appsettings.json:

  • {"IPAddressWhitelistConfiguration":?{"AuthorizedIPAddresses":?["::1",?//?IPv6?localhost"127.0.0.1",?//?IPv4?localhost"192.168.0.0/16",?//?Local?network"10.0.0.0/16",?//?Local?network]} }
  • IPWhiteListConfiguration.cs:

  • namespace?My.Web.Configuration {using?System.Collections.Generic;public?class?IPWhitelistConfiguration?:?IIPWhitelistConfiguration{public?IEnumerable<string>?AuthorizedIPAddresses?{?get;?set;?}} }
  • IIPWhiteListConfiguration.cs:

  • namespace?My.Web.Configuration {using?System.Collections.Generic;public?interface?IIPWhitelistConfiguration{IEnumerable<string>?AuthorizedIPAddresses?{?get;?}} }
  • Startup.cs:

  • public?class?Startup {//?...public?void?ConfigureServices(IServiceCollection?services){//?...services.Configure<IPWhitelistConfiguration>(this.Configuration.GetSection("IPAddressWhitelistConfiguration"));services.AddSingleton<IIPWhitelistConfiguration>(resolver?=>?resolver.GetRequiredService<IOptions<IPWhitelistConfiguration>>().Value);//?...}}
  • ClientIPAddressFilterAttribute.cs:

  • namespace?My.Web.Filters {using?System.Collections.Generic;using?System.Linq;using?System.Net;using?Microsoft.AspNetCore.Mvc;using?Microsoft.AspNetCore.Mvc.Filters;using?NetTools;using?My.Web.Configuration;public?class?ClientIPAddressFilterAttribute?:?ActionFilterAttribute{private?readonly?IEnumerable<IPAddressRange>?authorizedRanges;public?ClientIPAddressFilterAttribute(IIPWhitelistConfiguration?configuration){this.authorizedRanges?=?configuration.AuthorizedIPAddresses.Select(item?=>?IPAddressRange.Parse(item));}public?override?void?OnActionExecuting(ActionExecutingContext?context){var?clientIPAddress?=?context.HttpContext.Connection.RemoteIpAddress;if?(!this.authorizedRanges.Any(range?=>?range.Contains(clientIPAddress))){context.Result?=?new?UnauthorizedResult();}}} }

    點評區(qū)

    在 website 中設(shè)置ip白名單的功能非常常見,nuget上也有很多的開源中間件幫助實現(xiàn),比如:ClientIpAspNetCoreIIS,我在項目中也有用到白名單的功能,只不過是做到了 nginx 層。

    總結(jié)

    以上是生活随笔為你收集整理的Asp.Net Core 中如何设置 IP 白名单的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。