Form Authentication and URL Rewriting

2019-08-06 03:34发布

问题:

I'm trying to apply Custom Forms Authentication to my website to which I already applied Url Rewriting. using this code in Application_Start in global.asax file.

routes.MapPageRoute("bill-details",                 //Route Name
                    "{billno}",                     //URL with Parameters
                    "~/CallCenter/BillDetails.aspx" //Webforms page to Handle it.
                    );

But when I'm trying rewrite the Loginpage Url, I'm not able to rewrite it.

<forms loginUrl="/Login.aspx" name="MyCustomAuthentication" timeout="30"/>

The Actual Problem is, when I open a page it checks for Authentication and if it is not Authenticated, then it redirects to Login.aspx page.

it shows as mywebsite.com/Login.aspx?ReturnUrl="..." I'm not able to rewrite this. If I cannot Remove the Return URL then Can I place Login Instead of Login.aspx??

and If I use this Code from this post -> How to remove returnurl from url?

If I use this, then the control loops over and over and it says - Too many Redirects. I think the problem is, when the Control goes to Login Page like mywebsite.com/Login, then It checks for authentication and It redirects to Login.aspx page. and your code redirects again to Login page . This loop continues.

I also do not require the Return Url because I Users have to login first Inorder to access my website.

So Can you help me in Removing the Return Url also?? And also in URL Rewriting ??

I'm not able to solve this!!

Check my website. -> http://orders.maabookings.com UserId - temp Password - temp

In this In the Login page, Its displaying as http://orders.maabookings.com/Login.aspx?ReturnUrl=%2f I need this to be http://orders.maabookings.com/Login How can I do this??

回答1:

add a web.config to the folder where your Login.aspx placed

<?xml version="1.0"?>
<configuration>
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
  <location path="Login.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
</configuration>

and in Global.asax

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires upon attempting to authenticate the use

    Dim _URL As String = Request.Url.ToString

    If _URL.Contains("ReturnUrl") Then
        Response.Redirect("/Login")
    End If
End Sub

I think this helps you.