Страницы SSL под ASP.NET MVC - PullRequest
       53

Страницы SSL под ASP.NET MVC

80 голосов
/ 01 октября 2008

Как мне использовать HTTPS для некоторых страниц моего сайта на базе ASP.NET MVC?

Стив Сандерсон (Steve Sanderson) имеет довольно хороший учебник о том, как сделать это в СУХОЙ форме в Preview 4 по адресу:

http://blog.codeville.net/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/

Есть ли лучший / обновленный способ с Preview 5?,

Ответы [ 11 ]

92 голосов
/ 02 марта 2010

Если вы используете ASP.NET MVC 2 Preview 2 или выше , теперь вы можете просто использовать:

[RequireHttps]
public ActionResult Login()
{
   return View();
}

Хотя параметр порядка стоит отметить, так как упоминается здесь .

17 голосов
/ 13 июля 2009

MVCFutures имеет атрибут RequireSSL.

(спасибо Адаму за , отметив это в вашем обновленном посте)

Просто примените его к своему методу действия, используя 'Redirect = true', если вы хотите, чтобы http: // запрос автоматически становился https: //:

    [RequireSsl(Redirect = true)]

См. Также: ASP.NET MVC RequireHttps только в рабочей среде

9 голосов
/ 25 марта 2010

Как Amadiere писал , [RequireHttps] прекрасно работает в MVC 2 для ввода HTTPS. Но если вы хотите использовать HTTPS только для некоторых страниц, как вы сказали, MVC 2 не дает вам никакой любви - как только он переключает пользователя на HTTPS, он застревает там, пока вы не перенаправите его вручную. 1007 *

Подход, который я использовал, заключается в использовании другого пользовательского атрибута [ExitHttpsIfNotRequired]. При подключении к контроллеру или действию он будет перенаправлен на HTTP, если:

  1. Запрос был HTTPS
  2. Атрибут [RequireHttps] не был применен к действию (или контроллеру)
  3. Запрос был GET (перенаправление POST привело бы к всевозможным проблемам).

Он слишком велик для размещения здесь, но вы можете увидеть код здесь плюс некоторые дополнительные детали.

8 голосов
/ 26 августа 2009

Вот недавнее сообщение от Дэна Уолина на это:

http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx

Он использует атрибут ActionFilter.

3 голосов
/ 20 июля 2012

Для тех, кто не является поклонником атрибутивных подходов к разработке, вот фрагмент кода, который может помочь:

public static readonly string[] SecurePages = new[] { "login", "join" };
protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
    var pageName = RequestHelper.GetPageNameOrDefault();
    if (!HttpContext.Current.Request.IsSecureConnection
        && (HttpContext.Current.Request.IsAuthenticated || SecurePages.Contains(pageName)))
    {
        Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
    }
    if (HttpContext.Current.Request.IsSecureConnection
        && !HttpContext.Current.Request.IsAuthenticated
        && !SecurePages.Contains(pageName))
    {
        Response.Redirect("http://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
    }
}

Существует несколько причин, по которым следует избегать атрибутов, и одна из них заключается в том, что если вы хотите просмотреть список всех защищенных страниц, вам придется перепрыгнуть через все контроллеры в решении.

3 голосов
/ 01 октября 2008

Некоторые расширения ActionLink: http://www.squaredroot.com/post/2008/06/11/MVC-and-SSL.aspx Или атрибут действия контроллера, который перенаправляет на https: // http://forums.asp.net/p/1260198/2358380.aspx#2358380

2 голосов
/ 26 апреля 2013

Я перебрал этот вопрос и надеюсь, что моё решение может кому-нибудь помочь.

У нас мало проблем: - Нам нужно закрепить определенные действия, например, «LogOn» в «Account». Мы можем использовать встроенный атрибут RequireHttps, который великолепен, но он перенаправит нас обратно с помощью https: //. - Мы должны сделать наши ссылки, формы и такие "SSL осведомленными".

Как правило, мое решение позволяет указывать маршруты, которые будут использовать абсолютный URL, в дополнение к возможности указать протокол. Вы можете использовать этот подход для указания протокола «https».

Итак, сначала я создал перечисление ConnectionProtocol:

/// <summary>
/// Enum representing the available secure connection requirements
/// </summary>
public enum ConnectionProtocol
{
    /// <summary>
    /// No secure connection requirement
    /// </summary>
    Ignore,

    /// <summary>
    /// No secure connection should be used, use standard http request.
    /// </summary>
    Http,

    /// <summary>
    /// The connection should be secured using SSL (https protocol).
    /// </summary>
    Https
}

Теперь я создал свернутую вручную версию RequireSsl. Я изменил исходный код RequireSsl, чтобы разрешить перенаправление обратно на http: // urls. Кроме того, я поместил поле, которое позволяет нам определить, нужен ли нам SSL или нет (я использую его с препроцессором DEBUG).

/* Note:
 * This is hand-rolled version of the original System.Web.Mvc.RequireHttpsAttribute.
 * This version contains three improvements:
 * - Allows to redirect back into http:// addresses, based on the <see cref="SecureConnectionRequirement" /> Requirement property.
 * - Allows to turn the protocol scheme redirection off based on given condition.
 * - Using Request.IsCurrentConnectionSecured() extension method, which contains fix for load-balanced servers.
 */
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class RequireHttpsAttribute : FilterAttribute, IAuthorizationFilter
{
    public RequireHttpsAttribute()
    {
        Protocol = ConnectionProtocol.Ignore;
    }

    /// <summary>
    /// Gets or sets the secure connection required protocol scheme level
    /// </summary>
    public ConnectionProtocol Protocol { get; set; }

    /// <summary>
    /// Gets the value that indicates if secure connections are been allowed
    /// </summary>
    public bool SecureConnectionsAllowed
    {
        get
        {
#if DEBUG
            return false;
#else
            return true;
#endif
        }
    }

    public void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        /* Are we allowed to use secure connections? */
        if (!SecureConnectionsAllowed)
            return;

        switch (Protocol)
        {
            case ConnectionProtocol.Https:
                if (!filterContext.HttpContext.Request.IsCurrentConnectionSecured())
                {
                    HandleNonHttpsRequest(filterContext);
                }
                break;
            case ConnectionProtocol.Http:
                if (filterContext.HttpContext.Request.IsCurrentConnectionSecured())
                {
                    HandleNonHttpRequest(filterContext);
                }
                break;
        }
    }


    private 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))
        {
            throw new InvalidOperationException("The requested resource can only be accessed via SSL.");
        }

        // redirect to HTTPS version of page
        string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
        filterContext.Result = new RedirectResult(url);
    }

    private void HandleNonHttpRequest(AuthorizationContext filterContext)
    {
        if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("The requested resource can only be accessed without SSL.");
        }

        // redirect to HTTP version of page
        string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
        filterContext.Result = new RedirectResult(url);
    }
}

Теперь этот RequireSsl будет использовать следующую базу для вашего значения атрибута Requirements: - Игнорировать: не буду ничего делать. - Http: принудительное перенаправление на протокол http. - Https: принудительное перенаправление на протокол https.

Вы должны создать свой собственный базовый контроллер и установить для этого атрибута Http.

[RequireSsl(Requirement = ConnectionProtocol.Http)]
public class MyController : Controller
{
    public MyController() { }
}

Теперь, в каждом cpntroller / action, который вы хотите использовать SSL, просто установите этот атрибут с помощью ConnectionProtocol.Https.

Теперь давайте перейдем к URL-адресам: у нас мало проблем с механизмом маршрутизации URL. Вы можете прочитать о них больше на http://blog.stevensanderson.com/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/. Решение, предложенное в этом посте, теоретически хорошее, но старое, и мне не нравится подход.

Мои решения следующие: Создайте подкласс базового класса «Маршрут»:

открытый класс AbsoluteUrlRoute: маршрут { #region Ctor

    /// <summary>
    /// Initializes a new instance of the System.Web.Routing.Route class, by using
    ///     the specified URL pattern and handler class.
    /// </summary>
    /// <param name="url">The URL pattern for the route.</param>
    /// <param name="routeHandler">The object that processes requests for the route.</param>
    public AbsoluteUrlRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    {

    }

    /// <summary>
    /// Initializes a new instance of the System.Web.Routing.Route class, by using
    ///     the specified URL pattern and handler class.
    /// </summary>
    /// <param name="url">The URL pattern for the route.</param>
    /// <param name="defaults">The values to use for any parameters that are missing in the URL.</param>
    /// <param name="routeHandler">The object that processes requests for the route.</param>
    public AbsoluteUrlRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
        : base(url, defaults, routeHandler)
    {

    }

    /// <summary>
    /// Initializes a new instance of the System.Web.Routing.Route class, by using
    ///     the specified URL pattern and handler class.
    /// </summary>
    /// <param name="url">The URL pattern for the route.</param>
    /// <param name="defaults">The values to use for any parameters that are missing in the URL.</param>
    /// <param name="constraints">A regular expression that specifies valid values for a URL parameter.</param>
    /// <param name="routeHandler">The object that processes requests for the route.</param>
    public AbsoluteUrlRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                            IRouteHandler routeHandler)
        : base(url, defaults, constraints, routeHandler)
    {

    }

    /// <summary>
    /// Initializes a new instance of the System.Web.Routing.Route class, by using
    ///     the specified URL pattern and handler class.
    /// </summary>
    /// <param name="url">The URL pattern for the route.</param>
    /// <param name="defaults">The values to use for any parameters that are missing in the URL.</param>
    /// <param name="constraints">A regular expression that specifies valid values for a URL parameter.</param>
    /// <param name="dataTokens">Custom values that are passed to the route handler, but which are not used
    ///     to determine whether the route matches a specific URL pattern. These values
    ///     are passed to the route handler, where they can be used for processing the
    ///     request.</param>
    /// <param name="routeHandler">The object that processes requests for the route.</param>
    public AbsoluteUrlRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                            RouteValueDictionary dataTokens, IRouteHandler routeHandler)
        : base(url, defaults, constraints, dataTokens, routeHandler)
    {

    }

    #endregion

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        var virtualPath = base.GetVirtualPath(requestContext, values);
        if (virtualPath != null)
        {
            var scheme = "http";
            if (this.DataTokens != null && (string)this.DataTokens["scheme"] != string.Empty)
            {
                scheme = (string) this.DataTokens["scheme"];
            }

            virtualPath.VirtualPath = MakeAbsoluteUrl(requestContext, virtualPath.VirtualPath, scheme);
            return virtualPath;
        }

        return null;
    }

    #region Helpers

    /// <summary>
    /// Creates an absolute url
    /// </summary>
    /// <param name="requestContext">The request context</param>
    /// <param name="virtualPath">The initial virtual relative path</param>
    /// <param name="scheme">The protocol scheme</param>
    /// <returns>The absolute URL</returns>
    private string MakeAbsoluteUrl(RequestContext requestContext, string virtualPath, string scheme)
    {
        return string.Format("{0}://{1}{2}{3}{4}",
                             scheme,
                             requestContext.HttpContext.Request.Url.Host,
                             requestContext.HttpContext.Request.ApplicationPath,
                             requestContext.HttpContext.Request.ApplicationPath.EndsWith("/") ? "" : "/",
                             virtualPath);
    }

    #endregion
}

Эта версия класса "Маршрут" создаст абсолютный URL. Уловка, за которой следует предложение автора сообщения в блоге, заключается в использовании DataToken для указания схемы (пример в конце :)).

Теперь, если мы сгенерируем URL, например, для маршрута "Account / LogOn", мы получим "/ http://example.com/Account/LogOn" - это потому, что UrlRoutingModule видит все URL как относительные. Мы можем исправить что использует кастомный HttpModule:

public class AbsoluteUrlRoutingModule : UrlRoutingModule
{
    protected override void Init(System.Web.HttpApplication application)
    {
        application.PostMapRequestHandler += application_PostMapRequestHandler;
        base.Init(application);
    }

    protected void application_PostMapRequestHandler(object sender, EventArgs e)
    {
        var wrapper = new AbsoluteUrlAwareHttpContextWrapper(((HttpApplication)sender).Context);
    }

    public override void PostResolveRequestCache(HttpContextBase context)
    {
        base.PostResolveRequestCache(new AbsoluteUrlAwareHttpContextWrapper(HttpContext.Current));
    }

    private class AbsoluteUrlAwareHttpContextWrapper : HttpContextWrapper
    {
        private readonly HttpContext _context;
        private HttpResponseBase _response = null;

        public AbsoluteUrlAwareHttpContextWrapper(HttpContext context)
            : base(context)
        {
            this._context = context;
        }

        public override HttpResponseBase Response
        {
            get
            {
                return _response ??
                       (_response =
                        new AbsoluteUrlAwareHttpResponseWrapper(_context.Response));
            }
        }


        private class AbsoluteUrlAwareHttpResponseWrapper : HttpResponseWrapper
        {
            public AbsoluteUrlAwareHttpResponseWrapper(HttpResponse response)
                : base(response)
            {

            }

            public override string ApplyAppPathModifier(string virtualPath)
            {
                int length = virtualPath.Length;
                if (length > 7 && virtualPath.Substring(0, 7) == "/http:/")
                    return virtualPath.Substring(1);
                else if (length > 8 && virtualPath.Substring(0, 8) == "/https:/")
                    return virtualPath.Substring(1);

                return base.ApplyAppPathModifier(virtualPath);
            }
        }
    }
}

Поскольку этот модуль переопределяет базовую реализацию UrlRoutingModule, мы должны удалить базовый httpModule и зарегистрировать наш в web.config. Итак, под "system.web" установлено:

<httpModules>
  <!-- Removing the default UrlRoutingModule and inserting our own absolute url routing module -->
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="MyApp.Web.Mvc.Routing.AbsoluteUrlRoutingModule" />
</httpModules>

Вот так:).

Чтобы зарегистрировать абсолютный / протокольный маршрут, вы должны сделать:

        routes.Add(new AbsoluteUrlRoute("Account/LogOn", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new {controller = "Account", action = "LogOn", area = ""}),
                DataTokens = new RouteValueDictionary(new {scheme = "https"})
            });

Буду рад услышать ваши отзывы + улучшения. Надеюсь, это поможет! :)

Edit: Я забыл включить метод расширения IsCurrentConnectionSecured () (слишком много фрагментов: P). Это метод расширения, который обычно использует Request.IsSecuredConnection. Однако этот подход не будет работать при использовании балансировки нагрузки - поэтому этот метод может обойти это (взято из nopCommerce).

    /// <summary>
    /// Gets a value indicating whether current connection is secured
    /// </summary>
    /// <param name="request">The base request context</param>
    /// <returns>true - secured, false - not secured</returns>
    /// <remarks><![CDATA[ This method checks whether or not the connection is secured.
    /// There's a standard Request.IsSecureConnection attribute, but it won't be loaded correctly in case of load-balancer.
    /// See: <a href="http://nopcommerce.codeplex.com/SourceControl/changeset/view/16de4a113aa9#src/Libraries/Nop.Core/WebHelper.cs">nopCommerce WebHelper IsCurrentConnectionSecured()</a>]]></remarks>
    public static bool IsCurrentConnectionSecured(this HttpRequestBase request)
    {
        return request != null && request.IsSecureConnection;

        //  when your hosting uses a load balancer on their server then the Request.IsSecureConnection is never got set to true, use the statement below
        //  just uncomment it
        //return request != null && request.ServerVariables["HTTP_CLUSTER_HTTPS"] == "on";
    }
1 голос
/ 08 июня 2010

Это не обязательно относится к MVC, но это решение работает как для веб-форм ASP.NET, так и для MVC:

http://www.codeproject.com/KB/web-security/WebPageSecurity_v2.aspx

Я использовал это в течение нескольких лет, и мне нравится разделять проблемы и управление через файл web.config.

1 голос
/ 21 апреля 2009

Вот пост в блоге Адама Сальво , в котором используется ActionFilter.

1 голос
/ 21 апреля 2009

Вот сообщение в блоге Пабло М. Сибрано от января 2009 года, в котором собраны несколько методов, включая HttpModule и методы расширения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...