Thursday, March 22, 2012

Login Page

Dear All,
I'm working on login page. I want the page be redirected to admin page if user tick admin page
checkbox, otherwise, the page should directed to user page. But no matter the checkbox has been ticked or not, the page is redirected to user page. What's am I doing wrong?

Here is my code:

<%@dotnet.itags.org. Page Language="C#" Debug="true" %>
<%@dotnet.itags.org. import Namespace="System.Web.Security" %>
<script runat="server"
void Page_Load(Object sender, EventArgs e) {

if (chkAdminPage.Checked) {
ReturnUrl.Value = "/admin/index.aspx";
} else {
ReturnUrl.Value = "/user/index.aspx";
}

}

void LoginBtn_Click(Object sender, EventArgs e) {
FormsAuthentication.RedirectFromLoginPage(txtLoginID.Text, Persist.Checked);
}

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<input id="ReturnUrl" type="hidden" runat="server" />
<h2>Please Login:
</h2>
<table cellspacing="0" border="1" cellspadding="0">
<tbody>
<tr>
<td>
</td>
</tr>
<tr>
<td>
Login Id:</td>
<td>
<asp:TextBox id="txtLoginID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:</td>
<td>
<asp:TextBox id="txtPassword" runat="server" Width="151px" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Login
<asp:CheckBox id="chkAdminPage" runat="server" Text="Admin Page"></asp:CheckBox>
</td>
<td>
<asp:checkbox id="Persist" runat="server"></asp:checkbox>
<font size="2">Remember Password</font>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button id="loginBtn" onclick="LoginBtn_Click" Text="Login" runat="server" />
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html
Any Idea?
Thanks in advance
JoanneI'm not 100% sure, but you're calling the RedirectFromLoginPage method, and that method returns to whatever page caused Forms authentication to jump to the login page in the first place. (You can see the page that the login will return to in the URL when you're in the login page.)

I think what you want to do is set the authentication ticket manually (by calling FormsAuthentication.SetAuthCookie) and then manually directing to the appropriate page (using Response.Redirect).
I'm not 100% sure about this, but..

I have a similar setup in my site. I use the FormsAuthentication.RedirectFromLoginPage method as well. and if I specify a redirect link later in the code, it seems to do what those redirects say...

Here are a few things I think you should try: (btw, in future, so your code is easier to read, put it in

 tags, then it keeps its formatting)

actually... (leaving the stuff above incase its of use) I just read your code again, and now I'm confused.

This is why:

void Page_Load(Object sender, EventArgs e) {

if (chkAdminPage.Checked) {
ReturnUrl.Value = "/admin/index.aspx";
} else {
ReturnUrl.Value = "/user/index.aspx";
}

}

void LoginBtn_Click(Object sender, EventArgs e) {
FormsAuthentication.RedirectFromLoginPage(txtLoginID.Text, Persist.Checked);
}

the redirect script is in Page_Load, don't you want it to look like this:

void Page_Load(Object sender, EventArgs e) {

}

void LoginBtn_Click(Object sender, EventArgs e) {
FormsAuthentication.RedirectFromLoginPage(txtLoginID.Text, Persist.Checked);

if (chkAdminPage.Checked) {
ReturnUrl.Value = "/admin/index.aspx";
} else {
ReturnUrl.Value = "/user/index.aspx";
}
}

(I'd also suggest adding some custom validators to make sure the user exists, then encase the whole LoginBtn_Click code inside a if(isValid) statement, but I'm guessing your still testing and just havn't got to that stage yet?

Hope this helps..
-Ashleigh
I'm sorry, a mistake in my code before:

(Response.Redirect not return url [note, both ways *may* work])

void Page_Load(Object sender, EventArgs e) {

}

void LoginBtn_Click(Object sender, EventArgs e) {
FormsAuthentication.RedirectFromLoginPage(txtLoginID.Text, Persist.Checked);

if (chkAdminPage.Checked) {
Response.Redirect = "/admin/index.aspx";
} else {
Response.Redirect = "/user/index.aspx";
}
}

0 comments:

Post a Comment