Solved

C# Forms Authentication with Redirect Help Needed

Posted on 2006-07-17
2
270 Views
Last Modified: 2010-07-27
I've been using Forms authentication for a little while, but need help figuring out how to modify my web.config file to accomplish a redirect with variable information.

Here's my current web.config (pretty standard) regarding forms authentication:

<authentication mode="Forms">
      <forms loginUrl="./login.aspx" timeout="30"  slidingExpiration="true" />
</authentication>
<authorization>
        <deny users="?" /> <!-- Deny Non-Logged In users -->
</authorization>

If a User has a bookmark or receives a link to my site in an email and thereby reaches an .aspx page when they are in a logged-out state, I DO want them to be bounced to login.aspx.  BUT, I want to pass along the URL they were attempting to reach in the QueryString, so that if they subsequently successfully log in I can redirect them to the page they were attempting to reach in the first place.

If it were possible, I'd be creating a tag in web.config that looked like:

int numURLsegs = Convert.ToInt32( Request.Url.Segments.Length.ToString() );
string theQS   = Request.Url.Query.ToString();
theLoginURL = "./login.aspx?o=" + Request.Url.Segments[ ( numURLsegs - 1 ) ].ToString().Trim() + theQS.ToString().Trim();

<authentication mode="Forms">
      <forms loginUrl="<%=theLoginURL%>" timeout="30"  slidingExpiration="true" />
</authentication>

I know it's not likely that C# can be in web.config, but I'm hoping there's a way to pass the referrer somehow.

If it's not possible to use standard Forms authentication to do this, any suggestion would be appreciated.

Thanks,
Andy


 
0
Comment
Question by:smooga
  • 2
2 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17122590
The url should be passed already as part of the Forms mechanism. Check your querystring and there should be a key named URL with the original URL you targeted before being redirected to Login.aspx.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17122615
You should be able to bounce the user back to the original URL using:

    FormsAuthentication.RedirectFromLoginPage("username", false);

You should be able to double-check the URL you originally targeted using:

    string url = FormsAuthentication.GetRedirectUrl("username", false);
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question