在 WebApi 中需要 SSL? [英] Require SSL in WebApi?
问题描述
有没有办法为 WebApi 要求 SSL?一个属性?
Is there a way to require SSL for WebApi? An attribute?
我在 System.Web.Http
下没有看到适用的属性,类似于我们用于 MVC 的 RequireHttps
属性.如果有内置解决方案,我只是想避免滚动我自己的属性/消息处理程序.
I don't see an applicable attribute under System.Web.Http
, something like the RequireHttps
attribute we have for MVC. I'm just trying to avoid rolling my own attribute/ message handler if there is a built in solution.
推荐答案
您可以使用 RequireHttpsHandler 来自 WebAPIContrib 项目.基本上,它所做的只是检查传入的请求 URI 方案:
You can use the RequireHttpsHandler from WebAPIContrib project. Basically, all it does is to check the incoming request URI scheme:
if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
// Forbidden (or do a redirect)...
}
另外,Carlos Figueira 有 他的 MSDN 博客上的另一个实现.
Alternately, Carlos Figueira has another implementation on his MSDN blog.
这篇关于在 WebApi 中需要 SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!