ssl - 在 ASP.NET Web API ActionFilterAttribute 上测试本地主机

标签 ssl asp.net-web-api localhost

如何使用 ASP.NET Web API 在 ActionFilterAttribute 上测试本地主机?我想跳过 SSL 检查。

public class RequireHttpsAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var request = actionContext.Request;
            if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
            {
                throw new ValidationException(new SecureConnection());
            }

            base.OnActionExecuting(actionContext);
        }
    }

最佳答案

如果您只想测试请求 URI 是否为本地主机,那么 URI 上的 IsLoopback 应该可以正常工作。

但问题是……请求 URI 很容易被不是本地计算机的计算机欺骗。因此,任何远程计算机实际上都可以向您发送带有 localhost 主机 header 的请求。

更好的方法是使用 Filip 在其博客文章中的建议:

http://www.strathweb.com/2013/01/adding-request-islocal-to-asp-net-web-api/

这应该适用于 selfhost 和 webhost 以及客户端的 IP 地址是否是环回。

关于ssl - 在 ASP.NET Web API ActionFilterAttribute 上测试本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030822/

相关文章:

c# - 使用传入的参数作为搜索字段使用 Entity Framework Web API 创建动态搜索查询

c# - ASP.NET Web-Api 在本地工作但在 IIS7 上部署后不工作

authentication - SSL 如何影响 .NET Web Api 安全性?

asp.net-web-api - 您将使用 Visual Studio 2013 中的哪些项目类型来创建此结构

java - Tomcat/合流 : very long TLS handshake at start

google-app-engine - 如何通过代理本地测试gae golang urlfetch?

ruby-on-rails - 无法访问 http ://localhost:3000

laravel-4 - Vagrant 盒: default: Warning: Connection timeout.重试

encryption - SSL 证书加密与密码加密

javascript - 在 heroku 上运行 shadowsocks-nodejs