Reputation: 1014
Could anyone provide me with an example of "how to redirect user to a specific page after login" uisng asp.net role provider.
On my login page, I have implement the ILogOnView , but it only redirects users to the home page not the page I specified in the web.config file. Here are the code that I used in the web.config file.
Code from Logon.aspx
<asp:Login ID="Login1" runat="server" DestinationPageUrl="Templates.aspx" OnAuthenticate="Login1_Authenticate">
</asp:Login>
<forms
cookieless="UseCookies"
defaultUrl="Templates.aspx"
loginUrl="LogOn.aspx"
protection="All"
timeout="2880">
</forms>
public partial class LogOn : System.Web.UI.Page, ILogOnView
{
private LogOnPresenter presenter;
protected void Page_Load(object sender, EventArgs e)
{
presenter = LogOnPresenter.Create(this);
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (Membership.ValidateUser(Login1.UserName, Login1.Password) == true)
{
Login1.Visible = true;
Session["user"] = User.Identity.Name;
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
}
else
{
Response.Write("Invalid Login");
}
}
public void Redirect(string targetUrl)
{
//Response.Redirect("Templates.aspx");
}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Response.Redirect(Login1.DestinationPageUrl);
}
}
Any suggestions are welcome. Thanks & Regards,
Upvotes: 1
Views: 229
Reputation: 3284
If you are using an ASP:LOGIN control, you can specify "DestinationPageUrl" which is the destination page shown to the user when the log in is successful. This control will reside at "~/LogOn.aspx"
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx" CssClass="membershipcontrol" />
And add this to your code behind:
Public Partial Class LogOn
Inherits System.Web.UI.Page
Protected Sub Login1Function(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
Response.Redirect(Login1.DestinationPageUrl)
End Sub
End Class
EDIT:
OK, you are explicitly redirecting the user on line 17 using:
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
You can use this instead if you wish to send them to a static place:
Response.Redirect(Login1.DestinationPageUrl);
Upvotes: 1