Then I managed to block the application in another way, through I.P. Through some researches I saw that this was the best solution to apply in the system, which is a Owin system.
Solution:
Follows the solution:
public static string GetIP(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_OwinContext"))
{
return HttpContext.Current != null ? HttpContext.Current.Request.GetOwinContext().Request.RemoteIpAddress : null;
}
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : null;
}
return null;
}
public static bool AllowIP(this HttpRequestMessage request)
{
string whiteList= ConfigurationManager.AppSettings["whiteListIp"];
if (!IsNullOrEmpty(whiteList))
{
string[] ipList = whiteList.Split(';');
var ipAdress= request.GetIP();
bool ipAllowed = ipList.Where(x => x.Trim().Equals(ipAdress, StringComparison.InvariantCultureIgnoreCase)).Any();
return ipAllowed;
}
else return true;
}
Filter:
public class AllowedIpFilter: DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request.AllowIP())
{
return await base.SendAsync(request, cancellationToken);
}
return request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Access denied!");
}
}
but you want to restrict only to calls from the environment itself or a Whitelist?
– Leandro Angelo
Show your CORS implementation and if you want to restrict the entire application, some controllers, actions or methods
– Leandro Angelo
Then, after some time and a little more research, I managed to carry out the blocking through an i.p. filter where I have a Whitelist and through a delegating Handler I perform the filtering of i.p’s that can access my owin application.
– Guilherme Caixeta