Link to home
Start Free TrialLog in
Avatar of smooga
smooga

asked on

C# Forms Authentication with Redirect Help Needed

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


 
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial