应用于 AccountController.LogOn 时,MVC Preview 2 失败

发布于 2024-08-09 17:38:11 字数 1185 浏览 1 评论 0原文

当尝试将 [RequireHttps] 应用于 ASP.NET MVC 2 Preview 2 中的 AccountController.Logon 时,出现以下错误:

ASP.NET 检测到 URL 中存在无效字符。

这是因为 ASP.NET 已将我的请求从

http://example.com/admin 重写为

https://example.com/account/logon%3FReturnUrl=/admin

ASP.NET 本身添加了 ReturnURL(不是 ASP.NET MVC),但 RequireHttps 属性正在重定向并弄乱了 URL。

%3F 而不是 ? 正在破坏页面。

我认为从技术上来说这是 ASP.NET 中的一个错误。有解决方法吗? 我在想也许有一种方法可以处理 global.asax 中的“未经身份验证”事件 - 或者可能只是修复 RequireHttpsAttribute 的源。

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

    <authentication mode="Forms">
       <forms loginUrl="~/account/logon"/>
    </authentication>

这是一个类似但不同的问题

编辑:我刚刚尝试手动输入 http://example.com/accout/login?cat=dog,但它仍然重定向到无效的 URL:account/logon%3Fcat=dog >。我最初认为这与会员资格提供者和 [RequireHttps] 之间的冲突有关,但它看起来只是一个基本错误,所以我想我必须自己修复源。

When trying to apply [RequireHttps] to AccountController.Logon in ASP.NET MVC 2 Preview 2 I get the following error :

ASP.NET detected invalid characters in the URL.

This is because ASP.NET has rewritten my request from

http://example.com/admin to

https://example.com/account/logon%3FReturnUrl=/admin

It is ASP.NET itself that has added ReturnURL (not ASP.NET MVC), but it is the RequireHttps attribute that is redirecting and messing up the URL.

The %3F instead of ? is breaking the page.

I think its technically a bug in ASP.NET. Is there a workaround?
I'm thinking maybe a way to handle the 'unauthenticated' event in global.asax - or possibly just fixing the source for RequireHttpsAttribute.

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

    <authentication mode="Forms">
       <forms loginUrl="~/account/logon"/>
    </authentication>

Here's a similar, but different question

Edit: I just tried manually entering in http://example.com/accout/login?cat=dog and it still redirected to an invalid URL : account/logon%3Fcat=dog. I originally thought it was related to a conflict between the membership provider and [RequireHttps] but it looks like just a basic bug so I think I'll have to just fix the source myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云朵有点甜 2024-08-16 17:38:11

假设这是 ASP.NET MVC 2 Preview 2 中的临时错误,这就是我所做的:

  • 创建 RequireHttps2 类
  • 应用 [RequireHttps2] 属性而不是 [RequireHttps]

    公共类 RequireHttps2Attribute : FilterAttribute, IAuthorizationFilter
    {

    public virtual void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            抛出新的ArgumentNullException(“filterContext”);
        }
    
        如果(!filterContext.HttpContext.Request.IsSecureConnection)
        {
            HandleNonHttpsRequest(filterContext);
        }
    }
    
    protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
    {
        // 仅重定向 GET 请求,否则浏览器可能不会传播动词和请求
        // 主体正确。
    
        if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            抛出新的 InvalidOperationException("必须使用 SSL");
        }
    
        // 重定向到HTTPS版本的页面
        UriBuilder 构建器 = new UriBuilder()
        {
            方案=“https”,
            主机=filterContext.HttpContext.Request.Url.Host,
            路径=filterContext.HttpContext.Request.Path,
            查询 = filterContext.HttpContext.Request.QueryString.ToString()
    
            // 原始代码 : Path = filterContext.HttpContext.Request.RawUrl // 使用 RawUrl 因为它适用于 URL 重写
    
        };
    
        字符串 url = builder.ToString();
        filterContext.Result = new RedirectResult(url);
    }
    

    }

Assuming this is a temporary bug in ASP.NET MVC 2 Preview 2 this is what I did :

  • Created RequireHttps2 class
  • Applied the [RequireHttps2] attribute instead of [RequireHttps]

    public class RequireHttps2Attribute : FilterAttribute, IAuthorizationFilter
    {

    public virtual void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
    
        if (!filterContext.HttpContext.Request.IsSecureConnection)
        {
            HandleNonHttpsRequest(filterContext);
        }
    }
    
    protected virtual 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("MUST USE SSL");
        }
    
        // redirect to HTTPS version of page
        UriBuilder builder = new UriBuilder()
        {
            Scheme = "https",
            Host = filterContext.HttpContext.Request.Url.Host,
            Path = filterContext.HttpContext.Request.Path,
            Query = filterContext.HttpContext.Request.QueryString.ToString()
    
            // ORIGINAL CODE : Path = filterContext.HttpContext.Request.RawUrl // use RawUrl since it works with URL rewriting
    
        };
    
        string url = builder.ToString();
        filterContext.Result = new RedirectResult(url);
    }
    

    }

謌踐踏愛綪 2024-08-16 17:38:11

这在 ASP.NET MVC2 RC 中似乎已修复。

 /admin 

现在被重写为正确的 URL:

 /account/logon?ReturnUrl=/admin

This appears fixed in ASP.NET MVC2 RC.

 /admin 

now gets rewritten to the correct URL:

 /account/logon?ReturnUrl=/admin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文