1

我通过 Azure Api 管理门户公开了 Web Api,并且我已将策略添加到具有共享机密的 SET HTTP 标头。这样我想阻止对我的 api 后端(托管在 azure web 应用程序上的 api)的访问。现在我想知道我应该在我的 webapi 中的哪里添加自定义逻辑来检查共享的秘密?应该是actionFilters吗?

4

1 回答 1

0

最简单的地方是使用消息处理程序。它应该看起来像这样......

 public class RestrictClientHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            IEnumerable<string> secrets = null;
            request.Headers.TryGetValues("myapi-secret",out secrets);
            var secret = secrets.FirstOrDefault();
            if (secret == null || secret != "the secret" )
            {
                return Task.FromResult(new HttpResponseMessage(HttpStatusCode.Forbidden) { RequestMessage = request, Content=new StringContent("Direct access to this API is not allowed")});
            }
            return base.SendAsync(request, cancellationToken);
        }
    }

您可以通过将其添加到挂起配置对象的 MessageHandlers 集合来安装它。

于 2015-09-21T23:48:23.797 回答