Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These steps should allow you to implement the required behaviour.<br> To summarize: </p> <ol> <li>You are using routing but not MVC. My example will map a url like <a href="http://host/Mysite/userid/12345" rel="nofollow noreferrer">http://host/Mysite/userid/12345</a> onto a real page at <a href="http://host/Mysite/Pages/users.aspx?userid=12345" rel="nofollow noreferrer">http://host/Mysite/Pages/users.aspx?userid=12345</a>.</li> <li>You want to control access to these addresses, requiring the user to logon. My example has a page <a href="http://host/Mysite/login.aspx" rel="nofollow noreferrer">http://host/Mysite/login.aspx</a> with a standard login control, and the site is configured to use forms authentication. </li> </ol> <h2>Step 1</h2> <p>I've "hidden" the contents of the Pages folder using this web.config in the Pages folder: </p> <pre><code> &lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;httpHandlers&gt; &lt;add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/&gt; &lt;/httpHandlers&gt; &lt;pages validateRequest="false"&gt; &lt;/pages&gt; &lt;/system.web&gt; &lt;system.webServer&gt; &lt;validation validateIntegratedModeConfiguration="false"/&gt; &lt;handlers&gt; &lt;remove name="BlockViewHandler"/&gt; &lt;add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/&gt; &lt;/handlers&gt; &lt;/system.webServer&gt; &lt;/configuration&gt; </code></pre> <p>This ensures that if anyone uses a url like <a href="http://host/Mysite/Pages/users.aspx?userid=12345" rel="nofollow noreferrer">http://host/Mysite/Pages/users.aspx?userid=12345</a>, then they receive a standard 404 response. </p> <h2>Step 2</h2> <p>My top level web.config file contains (as well as all the standard stuff) this location element: </p> <pre><code> &lt;location path="userid"&gt; &lt;system.web&gt; &lt;authorization&gt; &lt;deny users="?"/&gt; &lt;/authorization&gt; &lt;/system.web&gt; &lt;/location&gt; </code></pre> <p>This prevents anonymous access to urls of the form <a href="http://host/Mysite/userid/12345" rel="nofollow noreferrer">http://host/Mysite/userid/12345</a> which means users will be automatically redirected to login.aspx, then if they provide valid credentials, they will be redirected to the correct location.</p> <h2>Step 3</h2> <p>For reference here is my global.asax: </p> <pre><code>&lt;script RunAt="server"&gt; 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") )); } &lt;/script&gt; </code></pre> <p>And here is my route handler:</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload