WebApi에 SSL이 필요합니까?
WebApi에 SSL을 요구하는 방법이 있습니까? 속성?
MVC에 대한 속성 System.Web.Http
과 같은에서 적용 가능한 속성이 표시되지 않습니다 RequireHttps
. 기본 제공 솔루션이있는 경우 내 자신의 속성 / 메시지 처리기를 롤링하지 않으려 고합니다.
당신은 사용할 수 있습니다 RequireHttpsHandler WebAPIContrib의 프로젝트를. 기본적으로 들어오는 요청 URI 체계를 확인하는 것뿐입니다.
if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
// Forbidden (or do a redirect)...
}
또는 Carlos Figueira가 MSDN 블로그에 또 다른 구현 을했습니다.
public class RequireHttpsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
}
}
}
ASP.NET Web API에 ASP.NET MVC RequireHttps 특성에 해당하는 것이 없다는 것은 당혹 스럽습니다. 그러나 MVC의 RequireHttps 를 기반으로 쉽게 만들 수 있습니다 .
using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
...
public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionContext");
}
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
HandleNonHttpsRequest(actionContext);
}
else
{
base.OnAuthorization(actionContext);
}
}
protected virtual void HandleNonHttpsRequest(HttpActionContext actionContext)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden);
actionContext.Response.ReasonPhrase = "SSL Required";
}
}
남은 일은 중복 코드가 얼마나 많은지에 대해 논쟁하는 것입니다.
다음 필터 클래스를 사용하여 작업 메서드가 SSL을 사용하도록 할 수 있습니다. 이것은 GET 메서드 또는 다른 동사를 사용하여 요청을 처리합니다. get 메서드 인 경우 브라우저 (위치 헤더 사용)를 새 브라우저로 리디렉션합니다. URI. 그렇지 않으면 https를 사용하는 메시지가 표시됩니다.
아래 코드는 AuthorizationFilterAttribute에서 상속 한 후 OnAuthorization 메서드를 재정의해야 함을 보여줍니다.
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");
}
}
몇 가지 조사 끝에 이것이 아마도 가장 적절한 응답이라고 판단했습니다. Html 권장 사양에도 불구하고 json, text 또는 xml을 제공하도록 업데이트 할 수 있습니다.
public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext context)
{
if (context.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
context.Response = new HttpResponseMessage(HttpStatusCode.UpgradeRequired);
context.Response.Headers.Add("Upgrade", "TLS/1.1, HTTP/1.1");
context.Response.Headers.Add("Connection", "Upgrade");
context.Response.Headers.Remove("Content-Type");
context.Response.Headers.Add("Content-Type", "text/html");
context.Response.Content = new StringContent("<html><head></head><body><h1>Http protocol is not valid for this service call.</h1><h3>Please use the secure protocol https.</h3></body></html>");
}
else base.OnAuthorization(context);
}
}
사양은 다음과 같습니다. RFC 2817
다음 코드를 사용할 수 있습니다. (자동으로 https로 리디렉션) http 기반 요청이있을 때 https로 리디렉션합니다.
To check it in visual studio you need to enable ssl in visual studio. This can be done using enable ssl property to true.
public class RequireHttpsAttribute: AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if(actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
// constructing the https url
var uriBuilder = new UriBuilder(actionContext.Request.RequestUri)
{
Scheme = Uri.UriSchemeHttps,
Port = 44353 // port used in visual studio for this
};
actionContext.Response.Headers.Location = uriBuilder.Uri;
}
}
}
Use this in Register method like this
config.Filters.Add(new RequireHttpsAttribute());
참고URL : https://stackoverflow.com/questions/11265710/require-ssl-in-webapi
'Development Tip' 카테고리의 다른 글
.live ()를 사용하여 jQuery UI 자동 완성 바인딩 (0) | 2020.11.14 |
---|---|
Java 프로그램 내에서 방금 시작한 프로세스의 PID를 얻는 방법은 무엇입니까? (0) | 2020.11.14 |
Angular 2에서 확인하는 방법 (0) | 2020.11.14 |
VS Code에서 Git 누락 – 소스 제어 공급자 없음 (0) | 2020.11.14 |
JavaScript 스포이드 (마우스 커서 아래의 픽셀 색상 표시) (0) | 2020.11.14 |