Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
  2. {
  3. // Forbidden (or do a redirect)...
  4. }
  5.  
  6. public class RequireHttpsAttribute : ActionFilterAttribute
  7. {
  8. public override void OnActionExecuting(HttpActionContext actionContext)
  9. {
  10. if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
  11. {
  12. actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
  13. }
  14. }
  15. }
  16.  
  17. using System;
  18. using System.Net.Http;
  19. using System.Web.Http.Controllers;
  20. using System.Web.Http.Filters;
  21.  
  22. ...
  23.  
  24. public class RequireHttpsAttribute : AuthorizationFilterAttribute
  25. {
  26. public override void OnAuthorization(HttpActionContext actionContext)
  27. {
  28. if (actionContext == null)
  29. {
  30. throw new ArgumentNullException("actionContext");
  31. }
  32.  
  33. if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
  34. {
  35. HandleNonHttpsRequest(actionContext);
  36. }
  37. else
  38. {
  39. base.OnAuthorization(actionContext);
  40. }
  41. }
  42.  
  43. protected virtual void HandleNonHttpsRequest(HttpActionContext actionContext)
  44. {
  45. actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden);
  46. actionContext.Response.ReasonPhrase = "SSL Required";
  47. }
  48. }
  49.  
  50. string _HtmlBody = string.Empty;
  51. UriBuilder httpsNewUri;
  52.  
  53. var _Request = actionContext.Request;
  54.  
  55. if (_Request.RequestUri.Scheme != Uri.UriSchemeHttps )
  56. {
  57.  
  58. _HtmlBody = "<p>Https is required</p>";
  59.  
  60. if (_Request.Method.Method == "GET"){
  61.  
  62. actionContext.Response = _Request.CreateResponse(HttpStatusCode.Found);
  63. actionContext.Response.Content = new StringContent(_HtmlBody, Encoding.UTF8, "text/html");
  64.  
  65. httpsNewUri = new UriBuilder(_Request.RequestUri);
  66. httpsNewUri.Scheme = Uri.UriSchemeHttps;
  67. httpsNewUri.Port = 443;
  68.  
  69. //To ask a web browser to load a different web page with the same URI but different scheme and port
  70. actionContext.Response.Headers.Location = httpsNewUri.Uri;
  71.  
  72.  
  73. }else{
  74.  
  75. actionContext.Response = _Request.CreateResponse(HttpStatusCode.NotFound);
  76. actionContext.Response.Content = new StringContent(_HtmlBody, Encoding.UTF8, "text/html");
  77.  
  78. }
  79. }
  80.  
  81. public class RequireHttpsAttribute: AuthorizationFilterAttribute
  82. {
  83. public override void OnAuthorization(HttpActionContext actionContext)
  84. {
  85. if(actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps){
  86. // constructing the https url
  87. UriBuilder uriBuilder = new UriBuilder(actionContext.Request.RequestUri);
  88. uriBuilder.Scheme = Uri.UriSchemeHttps;
  89. uriBuilder.Port = 44353 // port used in visual studio for this
  90. actionContext.Response.Headers.Location = uriBuilder.Uri;
  91. }
  92. }
  93. }
  94.  
  95. config.Filter.Add(new RequireHttpsAttribute());
  96.  
  97. public class RequireHttpsAttribute : AuthorizationFilterAttribute
  98. {
  99. public override void OnAuthorization(HttpActionContext context)
  100. {
  101. if (context.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
  102. {
  103. context.Response = new HttpResponseMessage(HttpStatusCode.UpgradeRequired);
  104. context.Response.Headers.Add("Upgrade", "TLS/1.1, HTTP/1.1");
  105. context.Response.Headers.Add("Connection", "Upgrade");
  106. context.Response.Headers.Remove("Content-Type");
  107. context.Response.Headers.Add("Content-Type", "text/html");
  108. 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>");
  109. }
  110. else base.OnAuthorization(context);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement