Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a webapp using C# and ASP. I have created a login page using forms authentication. I am kind of new to this, so please be mindful as to how you respond to me. I wish that there was a way to just simply upload the Project solution file because I may be having issues with the Web.config file as well as the login.aspx file. I am using Visual Studios 2019 to accomplish this. Please HELP

DC

What I have tried:

HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="AOIWebApp01.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="font-family:Arial">
<table style="border: 1px solid black">
    <tr>
        <td colspan="2">
            Please Enter Your Credentials
        </td>
    </tr>
    <tr>
        <td>
            User Name
        </td>    
        <td>
            :<asp:TextBox ID="txtUserName" runat="server">
            </asp:TextBox>
        </td>    
    </tr>
    <tr>
        <td>
            Password
        </td>    
        <td>
            :<asp:TextBox ID="txtPassword" TextMode="Password" runat="server">
            </asp:TextBox>
        </td>    
    </tr>
    <tr>
        <td>
                    
        </td>    
        <td>
            <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
        </td>    
    </tr>
    <tr>
        <td colspan="2">
            <asp:Label ID="lblMessage" runat="server" ForeColor="Red">
            </asp:Label> 
        </td>

    </tr>
</table>
<br />
<a href="Registration/Register.aspx">Click here to register</a> 
if you do not have a user name and password.
        </div>
    </form>
</body>
</html>
Login.aspx.cs
C#
using System;
using System.Web.Security;

namespace AutOSImageWeb01 {

	public partial class Login : System.Web.UI.Page {

		protected void Page_Load(object sender, EventArgs e) {}
	
		[Obsolete]
		protected void btnLogin_Click(object sender, EventArgs e) {
			if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text)) {
				FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
			}
			else {
				lblMessage.Text = "Invalid Username and/or Password.";
			}
		}
	}
}
Posted
Updated 31-Mar-20 6:02am
v2
Comments
MadMyche 30-Mar-20 12:49pm    
The form looks to be OK, but without seeing the code on Login.aspx.cs it is hard to tell if it is ok; particularly the btnLogin_Click method which would read the values from the form and then verify/validate
Derrick Chandler 31-Mar-20 9:39am    
Here is the code for the login.aspx.cs file:

using System;
using System.Web.Security;

namespace AutOSImageWeb01
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[Obsolete]
protected void btnLogin_Click(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
}
else
{
lblMessage.Text = "Invalid Username and/or Password.";
}
}
}
}
MadMyche 31-Mar-20 12:07pm    
RedirectFromLoginPage should take the user to the page they requested in the first place.
If you want this to do something different, comment that line out and add in your own Redirect routine
Derrick Chandler 31-Mar-20 12:23pm    
I have done so in the followng:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;


namespace AOIWebApp01
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnLogin_Click(object sender, EventArgs e)
{


if (txtUserName.Text == "enduser1, enduser2, enduser3")
{
Response.Redirect("client.aspx");
}

else if (txtUserName.Text == "ITAdmin1, ITAdmin2, ITAdmin3")
{
Response.Redirect("admin.aspx");
}

}


}

}
MadMyche 31-Mar-20 12:30pm    
You forgot to Authenticate them first, and then this should only be done IF authentication was successful

1 solution

In your login code after you validate the username and password you can use Response.Redirect("urltogoto"); to take them to a new page.
 
Share this answer
 
Comments
Derrick Chandler 31-Mar-20 9:41am    
Would I put the Response.Redirect line into the web.config file for the login.aspx fie? Forgive me as I am somewhat new to this.
ZurdoDev 31-Mar-20 9:49am    
It would be in the code behind file. There is no code in a webconfig or really even in an aspx file. Code goes in your .cs (C sharp) file.
Derrick Chandler 31-Mar-20 10:13am    
Gotcha. I want to do this based on a certain username and password. From the login page, if the users are end users, they will be redirected to a client page, but if they are IT Admins, they will be redirected to an Admin page. I want to do this without having to use an SQL database.
ZurdoDev 31-Mar-20 10:22am    
Then you have to hardcode everything, which is a really bad idea. But then you can do something like

... code to verify username and password...
...
if (Group == "IT Admins") {
Response.Redirect("somepage.aspx");
}
else if (Group == "Users")
{
Response.Redirect("SomeOtherPage.aspx");
}
...
etc.
Derrick Chandler 31-Mar-20 10:32am    
Where would I specify the group in VS 2019? If you're not too busy, could we possibly do a TeamViewer session?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900