C# 表单验证+;URL重写允许访问安全页面

C# 表单验证+;URL重写允许访问安全页面,c#,asp.net,url-rewriting,C#,Asp.net,Url Rewriting,我在ASP.NET中的URL重写和表单身份验证方面有问题。。。根据我在网上找到的文章,我创建了以下HttpModule: public class UrlRewriter : IHttpModule { private UrlRewriteConfigurationSection config; public UrlRewriter() { config = ConfigurationManager.GetSection("urlrewrites") a

我在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;
                }
            }
        }
    }
}
然而,问题是,这会以某种方式禁用授权!为了解释,假设我有以下重写规则:

URL模式:
^user/profile$
重写模式:
protected/profile.aspx

并假设文件夹
受保护
设置为拒绝匿名用户访问

现在,当运行
OnAuthorizerRequest
中的代码时,它会正确地重写到
protected/profile.aspx
的路径,但是,问题是,即使我没有登录,也会显示页面!如果我直接请求该页面(),则不允许访问该站点

我在网上找到的所有文章都说我需要在
AuthorizeRequest
中重写,而不是
AuthenticateRequest
BeginRequest

有什么想法吗


注意:我已经尝试将我的重写代码移动到
AuthenticateRequest
,这似乎是可行的,但是重定向到登录页面是不正确的(例如,它重定向到/login?returnUrl=protected/profile.aspx而不是login?returnUrl=user/profile)

Morten-这似乎是jro的副本-你说得对,它非常相似。然而,不同之处在于,在你的链接中,他通过在“nice”url中添加.aspx来解决问题。。这不适合我…莫顿-这似乎是一个重复的jro-你是对的,它非常相似。然而,不同之处在于,在你的链接中,他通过在“nice”url中添加.aspx来解决问题。。这对我不起作用。。。