è possibile utilizzare la seguente classe di filtro per forzare il metodo di azione a utilizzare SSL, questo gestirà la richiesta con un metodo GET o qualsiasi altro verbo, se è un metodo get reindirizzerà il browser (utilizzando l'intestazione della posizione) al nuovo URI. Altrimenti verrà visualizzato un messaggio per utilizzare https
Il codice seguente indica che è necessario sovrascrivere il metodo OnAuthorization dopo aver ereditato da AuthorizationFilterAttribute.
string _HtmlBody = string.Empty;
UriBuilder httpsNewUri;
var _Request = actionContext.Request;
if (_Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
_HtmlBody = "<p>Https is required</p>";
if (_Request.Method.Method == "GET"){
actionContext.Response = _Request.CreateResponse(HttpStatusCode.Found);
actionContext.Response.Content = new StringContent(_HtmlBody, Encoding.UTF8, "text/html");
httpsNewUri = new UriBuilder(_Request.RequestUri);
httpsNewUri.Scheme = Uri.UriSchemeHttps;
httpsNewUri.Port = 443;
//To ask a web browser to load a different web page with the same URI but different scheme and port
actionContext.Response.Headers.Location = httpsNewUri.Uri;
}else{
actionContext.Response = _Request.CreateResponse(HttpStatusCode.NotFound);
actionContext.Response.Content = new StringContent(_HtmlBody, Encoding.UTF8, "text/html");
}
}
fonte
2015-01-03 13:32:03
Questo è un articolo su questa domanda che vale la pena leggere: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/working-with-ssl-in-web-api – Tohid