using Forms Authentication

  • Thread starter Thread starter VR
  • Start date Start date
V

VR

Hi,

I am using Forms type of authentication, but having
problems redirecting users to default page after they get
authenticated.

My default page is default.aspx, but it's in 'public'
directory in realtion to my virtual directory:
"/public/default.aspx". In IIS I set the default document
to be "/public/default.aspx".

The problem is that once user is authenticated, ASP.NET
redirects him to "default.aspx" instead
of "public/default.aspx". MSDN says that "if there is no
original URL, Default.aspx is used" for
FormsAuthentication.GetRedirectUrl(...). How can I
customize the path to my default page?

Here is the code I use to authenticate a user and forward
him to the previously requested page:

FormsAuthenticationTicket oTicket =
new FormsAuthenticationTicket(szLogin, false, 1);

string szTicket =
FormsAuthentication.Encrypt(oTicket);

Response.Cookies.Add(
new HttpCookie(
FormsAuthentication.FormsCookieName, szTicket ));

string szURL =
FormsAuthentication.GetRedirectUrl(szLogin, false);

Response.Redirect(szURL);



Thanks,
VR
 
VR said:
Hi,

I am using Forms type of authentication, but having
problems redirecting users to default page after they get
authenticated.

What if the user doesn't want to go to your default page?

The way Forms Authentication works is that when a user tries to go to a page
which requires authentication and they're not authenticated, they get
redirected to your login page instead. The redirection includes the desired
page in the ReturnUrl query parameter. When they've authenticated, they're
meant to be redirected to the page they requested.

When MSDN says "if there is no original URL", they're referring to the case
where the user goes directly to your login page, so that there is no
ReturnUrl parameter. In this case, the default is to go to default.aspx. You
can change this by "manually" checking to see if there's a ReturnUrl
parameter. If there isn't, you can redirect to "/public/default.aspx".

BTW, the default document in IIS is just a file name and extension, not a
path. It simply says what file name IIS will request when the user
specifies, e.g., http://yoursite/ and doesn't specify a file name.
 
Thanks, John.

I wasn't really clear explaining the problem. I do realize that an empty
ReturnUrl parameter is an exceptional case, occuring when the user goes
directly to the authentication page. I was trying to figure out whether it's
possible to somehow change the behavior of GetRedirectUrl() to return a page
that I consider a default.

Just like you said, I can always check ReturnUrl and manually set it. I
didn't care much for this approach because that would imply that I am
hardcoding that page that I consider a default. So, I thought maybe there is
a way to alter the bahavior of GetRedirectUrl() by tweaking something in
either web.config or in IIS.

Or, I just realized, I guess I could just add a key to web.config myself,
read it in, and use it in my app, instead of hardcoding it.

Thanks again.

VR
 
Back
Top