1

I want, when I type http://localhost/Admin, to take me to the page http://localhost/Something/Login.aspx. How can I do this?

6
  • do you want the person to login to a protected area on your website or do you only want a redirect? Please clarify. Commented Dec 9, 2009 at 11:39
  • 2
    Could you elaborate? Do you want the user to be logged in to be able to access certain pages? Or do you simply want to perform a redirect? WebForms or MVC? Commented Dec 9, 2009 at 11:40
  • I only want to make a redirect to a specific URL. Commented Dec 9, 2009 at 11:43
  • Response.Redirect("~/Something/Login.aspx") should do the trick then Commented Dec 9, 2009 at 11:49
  • I don't want to redirect form code behind (C#). I want to rewrite the URL localhost/Admin into localhost/Something/Login.aspx. Commented Dec 9, 2009 at 11:56

1 Answer 1

7

What you are looking for is called Forms Authentication. A very short introduction follows.

You need to create a login page that makes a call like this, after verifying the identity of the user:

FormsAuthentication.RedirectFromLoginPage(userName);

Then you need to wire up the login page in the web.config file:

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Something/Login.aspx" />
  </authentication>
</system.web>

Furthermore, you will need to tell the framework that all URLs below ~/Admin/ requires the user to be authenticaticated. This can be done by adding an another web.config file within that folder:

<system.web>
  <authorization>
    <deny users="?" />
    <allow users="*" />
  </authorization>
</system.web>

Read the article linked above, or search the web for "ASP.NET forms authentication" and you will soon be on the right track.


EDIT 1 - If all you want to do is really to "make a redirect to a specific URL", then this is sufficient:

Response.Redirect("~/Something/Login.aspx")

From the URLs you mention in the your questions, it seems that you are trying to enforce some kind of authentication/authorization scheme. If this is true, forms authentication is a better answer.


EDIT 2 - If you want to rewrite, not redirect, requests from ~/Admin to ~/Something/Login.aspx you can do so by mapping a URL mapping in your root web.config file

<system.web>
    <urlMappings>
        <add url="~/Admin/Default.aspx" mappedUrl="~/Something/Login.aspx"/>
    </urlMappings>
</system.web>

In most setups, the web server will only pass the request to ASP.NET if the requested URL ends with a known suffix, such as .aspx. On approach to trick the web server to pass requests for ~/Admin to ASP.NET, is to use the "default document" feature in the web server. For this to work, you must add an empty file named Default.aspx in the ~/Admin folder.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.