title: .net Url rewriting for localization, issues with forms authentication and the loginUrl
body: <p>I'm using a HttpModule to rewrite the urls on a multi-lingual site. In the HttpModule, I'm adding a handler for the BeginRequest event, and looking for the first part of the path which contains the culture name.</p>
  
  <p>For example, /fr-ca/index.aspx will be rewritten to /index.aspx and set the thread's culture and ui culture to 3084. This works fine.</p>
  
  <p>Enter forms authentication. Forms authentication still works fine with the unmapped url, but if the user is unauthorized, it will redirect to the loginUrl as set in the authentication section of the web.config, and include the ?ReturnUrl= querystring parameter to handle redirecting back to the requested page once the user is authenticated.</p>
  
  <p>There are two problems here if the user requests a page in a language other than the default:</p>
  
  <ol>
  <li>The loginUrl ignores the path before it's rewritten</li>
  <li>The ReturnUrl parameter also ignored the raw path.</li>
  </ol>
  
  <p>It's equivalent to Request.Url.PathAndQuery instead of Request.RawUrl.</p>
  
  <p>I can't jump into the pipeline at the AuthorizeRequest event instead because then I'd have to protect against all possible culture values, because I'm using one web.config file with multiple location paths. This also doesn't fix the first issue.</p>
  
  <p>I've gone through the FormsAuthenticationModule in reflector and I see where I could change it to solve #1 and #2, but it's of course sealed.</p>
  
  <p>I've also browsed around a lot, but I can't see any workable solutions.</p>
  
  <p>The FormsAuthenticationModule is checking for a 401 header that's generated in UrlAuthorizationModule. If you look for the referrer (which is empty) on the loginUrl page it confirms that.</p>
  
  <p>Any thoughts?</p>
  
  <p><strong>EDIT #1</strong></p>
  
  <p>I'm using IIS 6, and IIS 7 is not an option.</p>
  
  <p><strong>EDIT #2</strong></p>
  
  <p>The login page wasn't picking up the default culture/ui culture because when I generated the local resource (design view: Tools > Generate Local Resource), the IDE added the following to the page directive:</p>
  
  <p>culture="auto" meta:resourcekey="PageResource1" uiculture="auto"</p>
  
  <p>Be careful with this in vs.net 2008! At least this solves one of the problems with the default culture not being referenced, but #1 and #2 are still outstanding.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I hoped that I could jump into one of the pipeline events to do my own redirect, but in Reflecting the System.Web.Security.UrlAuthorizationModule OnEnter method, I learned that once the 401 header is set, the method calls application.CompleteRequest, which as you can guess, brings us right to the EndRequest event. This is what the FA module jumps into the do the redirect, and I'm afraid I can't jump in front to do my own redirect! I'm surprised there aren't more people coming across this issue, or perhaps they haven't chimed in yet.</p>
  
  <hr>
  
  <p>Example:</p>
  
  <p>I have a member's section in the physical folder /members/ that's protected by forms authentication.</p>
  
  <p>In the web.config, I have:</p>
  
  <pre><code>&lt;authentication mode="Forms"&gt;
    &lt;forms loginUrl="~/members/login.aspx" timeout="40" /&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>  &lt;location path="members"&gt;
        &lt;system.web&gt;
            &lt;authorization&gt;
                &lt;deny users="?" /&gt;
                &lt;allow roles="Members" /&gt;
                &lt;deny users="*" /&gt;
            &lt;/authorization&gt;
        &lt;/system.web&gt;
    &lt;/location&gt;
  </code></pre>
  
  <p>When an unauthenticated user requests the index page of /members/, they get redirected to the loginUrl from the authentication section above by the FormsAuthenticationModule. It also appends the ?ReturnUrl parameter with the requested page.</p>
  
  <p>This works just fine if a user is browsing the website using the default culture, but my HttpModule sets the culture based on the presence of a culture name in the first part of the path.</p>
  
  <p>So, /fr-ca/members/index.aspx gets rewritten to /members/index.aspx and sets the culture/ui culture to Canadian French. Unforunately, the FormsAuthenticationModule sends the user to the loginUrl page with the rewritten url, not the original. So, the culture setting is lost and the redirect url is incorrect.</p>
  
  <p>Hope that helps @Greg</p>