Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require SSL in WebApi?

Is there a way to require SSL for WebApi? An attribute?

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.

like image 844
EBarr Avatar asked Jun 29 '12 16:06

EBarr


People also ask

Is SSL required for REST API?

By default ascd and REST uses the TLSv1. 2 protocol. Important: You must use the same SSL setting for the cluster management console and the RESTful web servers. If you enable SSL for one, you must also enable SSL for the other; if you disable SSL for one, SSL must be disabled for the other as well.

What is SSL in Web API?

SSL provides authentication by using Public Key Infrastructure certificates. The server must provide a certificate that authenticates the server to the client. It is less common for the client to provide a certificate to the server, but this is one option for authenticating clients.


2 Answers

public class RequireHttpsAttribute : ActionFilterAttribute {     public override void OnActionExecuting(HttpActionContext actionContext)     {         if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)         {             actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);         }     } } 
like image 144
The Light Avatar answered Oct 04 '22 11:10

The Light


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)... } 

Alternately, Carlos Figueira has another implementation on his MSDN blog.

like image 26
Teoman Soygul Avatar answered Oct 04 '22 11:10

Teoman Soygul