Solved

C# Forms Authentication with Redirect Help Needed

Posted on 2006-07-17
2
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.Net Session Question 2 49
Problem to copy file 14 92
Calculating percentage 2 49
Best book for Internet security 4 55
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…

710 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