In ASP.NET Core how do you check if request is local? -
in regular asp.net in view determine if current request localhost:
httpcontext.current.request.islocal
but can't find similar in asp.net 6/core/whatever meant called.
thanks in advance
i came across looking solution knowing if request local. unfortunately asp.net version 1.1.0 not have islocal method on connection. found 1 solution on web site called strathweb out of date too.
i have created own islocal extension, , seems work, can't have tested in circumstances, welcome try it.
public static class islocalextension { private const string nullipaddress = "::1"; public static bool islocal(this httprequest req) { var connection = req.httpcontext.connection; if (connection.remoteipaddress.isset()) { //we have remote address set return connection.localipaddress.isset() //is local same remote, local ? connection.remoteipaddress.equals(connection.localipaddress) //else remote if remote ip address not loopback address : ipaddress.isloopback(connection.remoteipaddress); } return true; } private static bool isset(this ipaddress address) { return address != null && address.tostring() != nullipaddress; } } you call in controller action using request property, i.e.
public iactionresult youraction() { var islocal = request.islocal(); //... code here } i hope helps someone.
Comments
Post a Comment