Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 455bc737ff
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (38 sloc) 1.664 kb
using System;
using System.Net;
using System.Web.Mvc;
namespace NuGetGallery
{
// This code is identical to System.Web.Mvc except that we allow for working in localhost environment without https.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class RequireRemoteHttpsAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
var request = filterContext.HttpContext.Request;
if (!request.IsLocal && !request.IsSecureConnection)
{
HandleNonHttpsRequest(filterContext);
}
}
private static void HandleNonHttpsRequest(AuthorizationContext filterContext)
{
// only redirect for GET requests, otherwise the browser might not propagate the verb and request
// body correctly.
if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
filterContext.Result = new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, Strings.SSLRequired);
}
else
{
// redirect to HTTPS version of page
string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
filterContext.Result = new RedirectResult(url);
}
}
}
}
Jump to Line
Something went wrong with that request. Please try again.