Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
How do you restrict requests so they only work if it is accessed by a specific domain

I have this ASP.NET Web API and I want to restrict access so it only works when called by specific host. I cannot, for what I know until now, secure it by token, because the WEB API url will be a postback url for a system that will call it automatically when a certain action is made. I have checked out CORS, but what CORS seems to do is to allow a specific domain to access the API. So, does this mean that my WEB API is already restricted for other domains? Then why I can access it by Postman locally, even if it is hosted in Azure?

I just want my service to allow calls from localhost and another specific domain only.

How do I achieve this?

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

CORS on its own only tells the browser / client apps to stop sending requests, in many ASP.Net Web API implementation it doesn't actually block the request pipeline. This is why Postman works, Postman doesn't execute the same pre-flight OPTIONS check first, it just send the request.

You should absolutely look into adding authentication to your API, Bearer token based authentication works well enough with and between APIs. Have a read over Secure a Web API with Individual Accounts and Local Login in ASP.NET Web API 2.2, but that is out of scope for this question.

At a conceptual level, you just need to intercept the incoming request in the OWIN pipeline before the API middleware and request the request if it doesn't match your rules. This should be using in conjunction with CORS so that browsers respond in a standard way.

For background, have a read over Block or limit unwanted traffic to Asp.Net Web Application. Usually we manage domain or IP level security filtering in the hosting architecture or routing. IIS or Azure hosts have many built in policies to help manage this.

You could implement this at a global level by adding the following OWIN request processor:

public void ConfigureOAuth(IAppBuilder app)
{
    app.Use((context, next) =>
    {
        string[] AllowedDomains = new string[] { "::1", "localhost", "mybusinessdomain.com" };

        if (!AllowedDomains.Contains(context.Request.Host.Value.ToLower()))
        {
            context.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
            return System.Threading.Tasks.Task.FromResult<object>(null);
        }

        return next();
    });

    // TODO: add in your other OWIN configuration AFTER the request filter.
}

If you are NOT using the OWIN hosting pipeline and you want to manage this globally then you could put a check into the global.asax.cs toi handle the Application_BeginRequest:

   private static readonly string[] AllowedDomains = new string[] { "::1", "localhost", "mybusinessdomain.com" };

   protected void Application_BeginRequest(Object sender, EventArgs e)
   {
       if >(!AllowedDomains.Contains(HttpContext.Current.Request.UserHostName.ToLower()))
       {
           HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
           HttpContext.Current.Response.End();

           // or transfer to a specific page
           // Server.Transfer("~/banned.aspx");
       }
   }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda