【问题标题】:Using Routing without MVC: authentication form在没有 MVC 的情况下使用路由:身份验证表单
【发布时间】:2009-01-01 20:00:54
【问题描述】:

现在我正在尝试使用 System.Web.Routing。一切都很好,但我不明白如何使表单身份验证与 url 路由(返回 url、重定向等)一起工作。谷歌什么也没说。帮助! :)

UPD:我忘了 - 我不使用 MVC。那就是问题所在。如何在没有 MVC 的情况下使用 rounig 和表单身份验证

UPD2:更多关于我的问题
我想得到:使用路由的 URL,例如“mysite.com/content/123”、“mysite.com/login/”等。重要的是让登录页面像“常规” ASP.NET 登录表单一样工作(未登录时重定向到安全区域登录,登录时重定向回安全区域)。
这就是我正在做的事情。
global.asax 上的Application_Start 中,像这样注册路由:

routes.Add("LoginPageRoute", new Route("login/", new CustomRouteHandler("~/login.aspx")));
routes.Add("ContentRoute", new Route("content/{id}", new ContentRoute("~/content.aspx"))
{
    Constraints = new RouteValueDictionary {{ "id", @"\d+" }}
});

CustomRouteHandlerContentRoute 的位置——简单的 IRouteHandler 类,就像: ...

public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
    var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
    return page;
}

...

一切似乎都很完美:当转到“/content/10” 时,我得到content.aspx,当转到“/login/” 时,我得到login.aspx。但是……
当我确保内容安全时(在web.config 中,使用deny=”?”),登录表单无法正常工作。
现在我无法访问 “/content/10” 页面:

0. 我正在浏览器中输入 “/content/10”
1. 网站重定向到“/login/?ReturnUrl=%2fcontent%2f10”。 (嗯……似乎所有的问题都从这里开始,对吧?:)
2. 我正在尝试登录。无论我输入什么凭据...
3. …网站将我重定向到 “login?ReturnUrl=%2fContent%2f10”(黄屏错误 - Access is denied. 描述:An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL。)
所以,问题是如何让 ASP.NET 理解真正的ReturnUrl 并在登录后提供重定向。

【问题讨论】:

    标签: asp.net authentication routing


    【解决方案1】:

    这些步骤应该允许您实现所需的行为。
    总结一下:

    1. 您正在使用路由但不是 MVC。我的示例会将http://host/Mysite/userid/12345 之类的网址映射到http://host/Mysite/Pages/users.aspx?userid=12345 的真实页面。
    2. 您想控制对这些地址的访问,要求用户登录。我的示例有一个带有标准登录控件的页面http://host/Mysite/login.aspx,并且该站点被配置为使用表单身份验证。

    步骤 1

    我已经使用 Pages 文件夹中的这个 web.config “隐藏”了 Pages 文件夹的内容:

      <?xml version="1.0"?>
      <configuration>
        <system.web>
          <httpHandlers>
            <add path="*" verb="*"
                type="System.Web.HttpNotFoundHandler"/>
          </httpHandlers>
          <pages validateRequest="false">
          </pages>
        </system.web>
        <system.webServer>
          <validation validateIntegratedModeConfiguration="false"/>
          <handlers>
            <remove name="BlockViewHandler"/>
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
          </handlers>
        </system.webServer>
      </configuration>  
    

    这样可以确保如果有人使用像 http://host/Mysite/Pages/users.aspx?userid=12345 这样的 URL,那么他们会收到标准的 404 响应。

    第二步

    我的顶级 web.config 文件包含(以及所有标准的东西)这个位置元素:

      <location path="userid">
        <system.web>
          <authorization>
            <deny users="?"/>
          </authorization>
        </system.web>
      </location>
    

    这可以防止匿名访问 http://host/Mysite/userid/12345 形式的 url,这意味着用户将被自动重定向到 login.aspx,然后如果他们提供有效的凭据,他们将被重定向到正确的位置。

    第三步

    这里是我的 global.asax 供参考:

    <script RunAt="server">
    
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
         }
    
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.RouteExistingFiles = true;
            routes.Add("UseridRoute", new Route
            (
               "userid/{userid}",
               new CustomRouteHandler("~/Pages/users.aspx")
            ));
        }
    
    </script>
    

    这是我的路由处理程序:

    using System.Web.Compilation;
    using System.Web.UI;
    using System.Web;
    using System.Web.Routing;
    using System.Security;
    using System.Web.Security;
    
    
    public interface IRoutablePage
    {
        RequestContext RequestContext { set; }
    }
    
    public class CustomRouteHandler : IRouteHandler
    {
        public CustomRouteHandler(string virtualPath)
        {
            this.VirtualPath = virtualPath;
        }
    
        public string VirtualPath { get; private set; }
    
        public IHttpHandler GetHttpHandler(RequestContext
              requestContext)
        {
            var page = BuildManager.CreateInstanceFromVirtualPath
                 (VirtualPath, typeof(Page)) as IHttpHandler;
    
            if (page != null)
            {
                var routablePage = page as IRoutablePage;
    
                if (routablePage != null) routablePage.RequestContext = requestContext;
            }
    
            return page;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我从 Google 搜索得到的第一个结果是 Frederiks 在forms authentication in ASP.NET MVC 上的出色帖子。请注意,该帖子与早期版本的 ASP.NET MVC 相关,您必须编写和测试代码。

      HTH,印地

      【讨论】:

      • 我更新了主题 - 我不使用 MVC。无论如何,谢谢你的回答。
      • 对不起,我没有真正帮助!也许如果你能提供更多关于你想要达到的目标的细节会有所帮助吗? Like - + Login.aspx - 使用会员 API 登录用户,重定向到安全区域。 + 安全区域(登录用户可访问)像这样?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多