Jump to content

Login In source code assist plz.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Tonko

Tonko

    Newbie

  • Members
  • Pip
  • 5 posts
Hi, I'm very new to C# and currently learning ASP.NET 2.0. I know ASP.NET 2.0 offers a login wizard and such, but i want to learn how to create my own codes. I've been looking for weeks on the web and only found info in VB and the wizard. No site i can find that would give me clues or ideas on which codes i would use. I finally gave up searching the web and started seeking programming forums. This website appears to have very knowledgeable C# experts so I'm here now seeking assistants. If anyone can help or know a site that can walked me through just writing a standard login/pass(basically the codes after clicking submit/login) source code is greatly appreciated.

Thanks in advance,

Tonko

#2
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
A lot of people seem to be trying this project. It is a good one for practice with the System.IO namespace. I haven't really seen a good example on the net anywhere (there could certainly be one), but It is fairly straigt forward. All it takes is some sort of storage file for your password and username info and then a GUI which will work for login and creating/deleting users. Depending on what sort of storage file you use for the user names and the passwords you will need to find a way to switch the information into some other sort of data (usually conversion to some sort of numeric data; ASCII, Unicode, etc.) This way if your files security was ever compromised the info would not mean much to the passive looker. There are classes in C# to deal with this as well as in SQL if you plan to use a data base for the storage of info (For a beginner I would recommend a simple XML file or even a text file.) Other than that this is a pretty straight forward project; input the data from the user and then loop through the files info and see if they match.
good luck

#3
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yep, gaylo's got the basics there for you. It's worth noting that the System.Xml namespace provides excellent support for reading and writing XML files, and provides a useful and fast alternative to a potentially complex database such as SQL.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#4
shruti

shruti

    Newbie

  • Members
  • PipPip
  • 16 posts
I don't know too much about it .I think you should search in google.

#5
Tonko

Tonko

    Newbie

  • Members
  • Pip
  • 5 posts
Thx for the advices, i'll try it.

#6
Tonko

Tonko

    Newbie

  • Members
  • Pip
  • 5 posts
Just 2 quick questions. Using ASP.NET 2.0/C# if i linked to a database, do i still have to write a sqlconnection to that database to retrieve info? Or could i just ignore new connection statement and use the GET FROM commands?

And if i'm not link to a database, will writing the sqlconnection allow me to link to any database i have available to my webpage in ASP.NET 2.0?

Sorry if these questions sound confusing, but i'm pretty confused myself since i'm learning this stuff off of dummy books. >.>

thx again,

Tonko

#7
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
For ASP.NET your connection string statement should go in your app.config file which should be under your references in the solution explorer (im assuming your using Visual Studio), or in your coding files themselves if you prefer. You can add it graphically or do it manually. It should look something like this if its in your app.config file (usually standard practice):
<connectionStrings>
        <add name="WpfBrowserApplication1.Properties.Settings.PasswordConnectionString"
            connectionString="Data Source=C:\Documents and Settings\Owner.Gir\My Documents\Password.sdf;Password=skiforlife"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>
As you can tell this is pretty specific to the actual database you are using. For legacy reasons, there are many different ways to write a connection string that does exactly the same thing; ie the keywords Data, Source, Server, Address, Addr, and Network Address are all synonyms. This can make things seem very confusing:) Once you do this you need to declare and instantiate a connection object. Here is an example of the instantiation:
connectionString = WebConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
You must then open up the connection in order to execute any commands against the server, and close it again as soon as possible after retrieving or editing the information. It is also good practice to include exception handling so that your connection closes even if there are errors;)
I hope this info helps clear up the confusion about the connection object. There are many ways to actually retrieve and work with data so don't be surprised if it takes a while to figure it out the way you want it. Good luck!

#8
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
just an additional question regarding the sqlconnection, when you see ="Data Source=C:\Documents and Settings\Owner.Gir\My Documents\Password.sdf;Password=skiforlife,

instead of C:\ can you put in a UNC name like \\mycomputername\folder1\etc etc..?

#9
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Yes:) This would actually be the more common case but the one I pasted was from the local server I use for development purposes.

#10
Tonko

Tonko

    Newbie

  • Members
  • Pip
  • 5 posts
Thx aton for your help guys, especially to you gaylo565.

I seem to understand the concept better now. Thou i can't for the life of me figure out why i'm getting this error.


------------
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0115: 'ASP.login_aspx.GetTypeHashCode()': no suitable method found to override

Source Error:

Line 793:
Line 794: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 795: public override int GetTypeHashCode() {
Line 796: return 755768783;
Line 797: }
------------------------

i have no idea what this means ><. here is the code i'm using.

protected void Loginbtn_Click(object sender, EventArgs e)
{

bool blnFound = false;

SqlConnection con1 = new SqlConnection();
con1.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\WebServer\htdocs\login4\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
con1.Open();
SqlCommand cmd=new SqlCommand("select * from Clientinfo",con1);
SqlDataReader dr=cmd.ExecuteReader();

while(dr.Read())
{

if(dr["Username"].ToString()==usernametxt.Text)
{
if (dr["Password"].ToString().ToString() == Passwordtxt.Text)
{
blnFound = true;

Response.Redirect(Main.aspx);

}
}
}


if(!blnFound)
lblMessage = "Invalid login information, please try again.";


dr.Close();
con1.Close();
}

-------


any ideas what i'm doing wrong? thx again!

Tonko~

#11
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Looks like you have an extra ToString method on the password check. Also looks like you should add a check to be sure that your password entered matches the username entered not just a database match. The problem your getting is with some code your not showing though. This error occurs when a method is marked as an override but there is no suitable method to override. I would have to see the rest of your code to know exactly whats wrong. You can look up the compiler error message number with msdn and figure out exactly what is going wrong;)

#12
Tonko

Tonko

    Newbie

  • Members
  • Pip
  • 5 posts
I'm sorry, here is the whole code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;



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

}

protected void Loginbtn_Click(object sender, EventArgs e)
{

bool blnFound = false;

SqlConnection con1 = new SqlConnection();
con1.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\WebServer\htdocs\login4\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
con1.Open();
SqlCommand cmd=new SqlCommand("select * from Clientinfo",con1);
SqlDataReader dr=cmd.ExecuteReader();

while(dr.Read())
{

if(dr["Username"].ToString() == usernametxt.Text)
{
if (dr["Password"].ToString() == Passwordtxt.Text)
{
blnFound = true;

Response.Redirect(Main.aspx);

}
}
}


if(!blnFound)
lblMessage = "Invalid login information, please try again.";


dr.Close();
con1.Close();
}

protected void Resetbtn_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
usernametxt.Text = "";
Passwordtxt.Text = "";
}
}
---------------------
Design code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login</title>
<style type="text/css">

.style1
{
width: 100%;
}
.style2
{
width: 34px;
}
.style6
{
width: 4px;
}
.style4
{
width: 362px;
}
.style5
{
width: 81px;
}
.style7
{
width: 86px;
}
.style8
{
width: 226px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<div>

</div>
<table cellpadding="10" class="style1">
<tr>
<td class="style8" rowspan="5">
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="54px"
style="margin-top: 20px" />
</td>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
</td>
<td>
 </td>
</tr>
<tr>
<td colspan="2">
SOFTPRO LOGIN</td>
<td>
 </td>
</tr>
<tr>
<td class="style2">
Username:</td>
<td class="style6">
<asp:TextBox ID="usernametxt" runat="server" Width="237px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFV1" runat="server"
ControlToValidate="usernametxt" ErrorMessage="Please enter the username.">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Password:</td>
<td class="style6">
<asp:TextBox ID="Passwordtxt" runat="server" Width="237px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFV2" runat="server"
ControlToValidate="Passwordtxt" ErrorMessage="Please enter the password.">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
 </td>
<td class="style6">
 </td>
<td>
 </td>
</tr>
</table>
<table class="style1">
<tr>
<td class="style4">
 </td>
<td class="style5">
<asp:Button ID="Resetbtn" runat="server" CausesValidation="False"
onclick="Resetbtn_Click" style="margin-left: 0px" Text="Reset" Width="68px" />
</td>
<td class="style7">
<asp:Button ID="Loginbtn" runat="server" onclick="Loginbtn_Click" Text="Login"
Width="68px" />
</td>
<td>
 </td>
</tr>
</table>
</form>
</body>
</html>

---------------------
Nothing in my web.config that i changed. I correct the extra Tostring and now i'm getting this..


Directory Listing -- /login4/

Sunday, July 27, 2008 04:16 PM <dir> App_Data
Monday, July 28, 2008 05:26 PM 437 Main.aspx
Monday, July 28, 2008 05:26 PM 449 Main.aspx.cs
Tuesday, July 29, 2008 04:26 PM 8,146 web.config
Wednesday, August 06, 2008 03:26 PM 1,628 Login.aspx.cs
Wednesday, August 06, 2008 03:26 PM 3,369 Login.aspx

Version Information: ASP.NET Development Server 9.0.0.0


I don't know why this is happening. ><;" Thx again.

Tonko