Thursday, March 22, 2012

Windows Forms Authentication: Redirecting to a specific page

I have a logon form from which I want to do either of the following:
(a) redirect users to their original request
(b) send to a specific page if their original request was for the logon form because they no longer need to logon.
My logon form is named "LogonForm.aspx". I want to redirect Users to "SearchForm.aspx" in the event that optionbis true. What should I be testing to see where the redirection willoccur and how can I set that value to "SearchForm.aspx" if it is notpointing there? Currently, I get a "The resource cannot be found" errorfor "default.aspx" when I call the following message in the logonbutton click handler:
protected void OnProcessLogon( string username, string password )
{ // Check the database for credentials.
Ceoimage.Basecamp.Data.Models.UserModel user =Ceoimage.Basecamp.Data.User.LogOn( CeoParams.DbFactory, SafeUserName,SafePassword );
_InvalidLogonLabel.Visible = ( user.UserId <= 0 );
if( _InvalidLogonLabel.Visible )
return;

System.Web.Security.FormsAuthentication.RedirectFromLoginPage(user.UserName, false);
//Response.Redirect( "SearchForm.aspx" );
}

You can manually check to see if they redirected this page. This is the same logic that the RedirectFromLogin page uses when determining where to send the user back to.
if ( _InvalidLoginLabel.Visible )
return;
//means they didn't request a page initially.
if ( Request.QueryString["ReturnUrl"] == null )
{
FormsAuthentication.SetAuthCookie( user.UserName, false );
Response.Redirect( "SearchForm.aspx" );
}
else
{
FormsAuthentication.RedirectFromLoginPage( user.UserName, false );
}
bill


Thanks! So there is no way to change the default page RedirectFromLoginPage sends you to?

Silly me for calling this "Windows Forms". Just can't shake my roots.


Unfortunately it is hard coded in ASP.NET 1.1. However, in ASP.NET 2.0 it is possible by setting the DefaultUrl of the Forms Authentication section of the web.config.
bill

0 comments:

Post a Comment