表单身份验证+ URL重写可以访问安全页面

时间:2009-12-03 18:24:39

标签: c# asp.net url-rewriting

我在ASP.NET中重写URL和表单身份验证有问题...根据我在网上找到的文章,我创建了以下HttpModule

public class UrlRewriter : IHttpModule
{
    private UrlRewriteConfigurationSection config;

    public UrlRewriter()
    {
        config = ConfigurationManager.GetSection("urlrewrites") as UrlRewriteConfigurationSection;
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        httpApplication.AuthorizeRequest += new EventHandler(OnAuthorizeRequest);
    }

    private void OnAuthorizeRequest(object sender, EventArgs e)
    {
        string requestedPath = HttpContext.Current.Request.Path;

        foreach (UrlRewriteRule rule in config.UrlRewriteRules)
        {
            RegexOptions options = config.IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;

            Regex regex = new Regex(rule.UrlPattern, options);
            Match match = regex.Match(requestedPath);

            if (match.Success)
            {
                string newPath = regex.Replace(requestedPath, rule.RewritePattern);
                if (!String.IsNullOrEmpty(newPath))
                {
                    HttpContext.Current.RewritePath(newPath);
                    return;
                }
            }
        }
    }
}

但问题是,这会以某种方式禁用授权!为了解释假设我有以下重写规则:

UrlPattern:^user/profile$ RewritePattern:protected/profile.aspx

并假设文件夹protected设置为拒绝匿名用户访问..

现在,当OnAuthorizeRequest中的代码运行时,它正确地重写了protected/profile.aspx的路径,然而,问题是我显示了页面,即使我没有登录!如果我直接请求页面(http://localhost/site/protected/profile.aspx),则不允许访问该网站..

我在网上找到的所有文章都说我需要在AuthorizeRequest而不是AuthenticateRequestBeginRequest进行重写。

有什么想法吗?

注意:我已经尝试将我的重写代码移动到AuthenticateRequest这似乎有效,但重定向到登录页面是不正确的(例如,它重定向到/login?returnUrl=protected/profile.aspx而不是登录资讯?RETURNURL =用户/配置文件)

0 个答案:

没有答案
相关问题