2

我需要允许用户通过我的 asp.net MVC 应用程序中的表单更改他们的密码。我的第一个想法是用 RequireHttps 属性装饰 ChangePassword 操作。

但是,在属性启动并返回“请求的资源只能通过 SSL 访问”之前,我仍然必须发送未加密的密码。这违背了目的,不是吗?

我确信我只是感到困惑,RequireHttps 很有用;我想知道是否有办法使用 RequireHttps 来实现我的目标。或者,我想知道任何其他方式来实现它。

更新:

由于下面的答案,我现在有了一些选择 - 我可以使用 https 在 iframe 中加载密码输入,这意味着来自它的任何帖子都将被加密。否则,我可以在构造帖子 url 的代码中将协议设置为 https:

var url = '@Url.Action("changePassword", "Security", new { area = "" }, "https")'

我不确定哪个更好,但我会尝试第二个 - 欢迎任何评论。

4

3 回答 3

6

您的应用程序无法控制是否启用 SSL。这仅取决于 Web 服务器配置。您唯一能做的就是确保您的应用程序不信任未在线加密的数据。RequireHttps 就是这样做的。使用此属性修饰的操作将永远不会处理以纯文本形式发送的数据。

于 2013-01-03T16:53:34.983 回答
2

的真正用例是仅在请求身份验证时RequireHttpsAttribute强制执行该方案。https://并非在所有情况下。RequireHttpsAttribute只实现了IAuthenticationFilter接口的OnAuthentication方法。

由于仅在 InvokeAuthenticationFilters 方法中调用OnAuthentication方法,因此我不会使用该RequireHttpsAttribute属性。

为了正确执行https://某些控制器或操作,我将创建自己的属性,基于ActionFilterAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class EnforceHttpsActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (new[] { "GET", "HEAD" }.Any(verb => String.Equals(filterContext.HttpContext.Request.HttpMethod, verb, StringComparison.OrdinalIgnoreCase))) ;
        {
            string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
            filterContext.Result = new RedirectResult(url);
        }
    }
}

要对整个站点强制执行,您可以从我用于示例应用程序实例的web.config 标记https://中获得灵感。*.azurewebsites.net

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTPS Redirect in Azure">
          <match url="(.+)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^(.+)\.azurewebsites.net(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
于 2017-10-20T15:20:18.197 回答
1

注意:该[RequireHttps]属性不处理HEAD请求,而是给出异常,因此某些蜘蛛程序或预取工具在尝试访问您的站点时会出错。

无论如何,最好在 IIS 中使用rewrite 模块做这样的事情。

    <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>

取自这里:https ://blogs.technet.microsoft.com/dawiese/2016/06/07/redirect-from-http-to-https-using-the-iis-url-rewrite-module/

重要提示:迁移到新服务器时不要忘记重新安装重写模块 - 如果您忘记了,您得到的错误有点迟钝!

于 2017-06-03T02:34:26.733 回答