Using Routing without MVC: authentication form

后端 未结 2 989
暖寄归人
暖寄归人 2020-12-16 07:34

Now I\'m trying to work with System.Web.Routing. All is just fine, but I can\'t understand how to make form authentication work with url routing (return url, redirection, e

相关标签:
2条回答
  • 2020-12-16 08:26

    The first result I got from a Google search is Frederiks excellent post on forms authentication in ASP.NET MVC. Note that the post was relevant for an early version of ASP.NET MVC, you will have to write and test the code.

    HTH, Indy

    0 讨论(0)
  • 2020-12-16 08:28

    These steps should allow you to implement the required behaviour.
    To summarize:

    1. You are using routing but not MVC. My example will map a url like http://host/Mysite/userid/12345 onto a real page at http://host/Mysite/Pages/users.aspx?userid=12345.
    2. You want to control access to these addresses, requiring the user to logon. My example has a page http://host/Mysite/login.aspx with a standard login control, and the site is configured to use forms authentication.

    Step 1

    I've "hidden" the contents of the Pages folder using this web.config in the Pages folder:

      <?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>  
    

    This ensures that if anyone uses a url like http://host/Mysite/Pages/users.aspx?userid=12345, then they receive a standard 404 response.

    Step 2

    My top level web.config file contains (as well as all the standard stuff) this location element:

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

    This prevents anonymous access to urls of the form http://host/Mysite/userid/12345 which means users will be automatically redirected to login.aspx, then if they provide valid credentials, they will be redirected to the correct location.

    Step 3

    For reference here is my 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>
    

    And here is my route handler:

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题