Current location: Hot Scripts Forums » Programming Languages » ASP.NET » Login in C#

Login in C#

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Jul 2003
Location: Sweden
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Login in C#

I want to add a login function for my ASP.net site. I guess I need a database for usernames and passwords, but I donīt have a clue how I should implement this. All I know is this: The pages I want to request a login for should be in a seperate folder, and I should do something in Web.config.

Could someone walk me through this process? Is there any good guides?
Reply With Quote

Featured Marketplace Scripts (view more)

  • Simple Forum PHP

    Simple Forum PHP script - easy to put on your website

  • File Watch

    This script monitors your website files for any changes.

  • Javascript iTunes Store …

    Instantly generate your own Customized Niche Store for Top Charting iTunes, …

  • MobileClone

    Your Own Mobile Store with AT&T, Sprint, T-Mobile, Verizon...

  #2 (permalink)  
Old
Coding Addict
 
Join Date: Jun 2003
Location: Maryland, US
Posts: 268
Thanks: 0
Thanked 0 Times in 0 Posts
Here's some of my code.


Login.aspx (note: this is just the code, not the markup part with the html)
Code:
		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
                     if(Page.IsValid) 
			{
				Users users = new Users();
				bool auth;
				auth = users.AuthenticateUser(txtUsername.Text,txtPassword.Text);

				if(auth) 
				{
	// authenticated was successful, lets create the authentication cookie and redirect them to another place (either where they were trying to go to or default.aspx)
				FormsAuthentication.RedirectFromLoginPage(txtUsername.Text,false);
				}
				else 
				{ 
					lblMessage.Text = "Account information was incorrect. Please try again!";
				
				}
			}
			else 
			{
				lblMessage.Text = "Missing some fields. Please try again.";
			}
}

My authentication method that gets called from the code above
Code:
		public bool AuthenticateUser(string username, string password) 
		{
			bool authenticated;
			SqlDataReader reader;
			SqlConnection conn = new SqlConnection(Globals.ConnectionString());
			SqlCommand cmd = new SqlCommand("AuthenticateUser",conn);

			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@username",username);
			cmd.Parameters.Add("@password",password);
			conn.Open();
			reader = cmd.ExecuteReader();
			
			if(reader.Read()) 
			{
				authenticated = true;
			}
			else 
			{
				authenticated = false;
			}

			reader.Close();
			conn.Close();
			conn.Dispose();
			

			return authenticated;
		}

And finally, the web.config file. Add these lines between your system.web tags.
Code:

    <authentication mode="Forms">
		<forms name="FormsEClient" loginUrl="login.aspx" protection="All" />
	</authentication> 



    <authorization>
	
        <deny users="?" />

    </authorization>
The web.config is one of the most important parts. The first tag in it tells asp.net to use forms authentication for this application. The loginUrl parameter sets the login form that they will automatically be redirected to if they are not logged in.

The authorization tag is just saying, "asp.net, deny all users who are anonymous (the question mark means anonymous)".

What you may want to do is implement the code in the web.config first, create a login.aspx page and just test it out. If you go to a page in your application when you are not authenticated and you end up getting redirected to login.aspx, then it's working.
__________________
Shane Bauer
Microsoft Certified Professional (MCP) - ASP.NET
ASP/ASP.net, C#, VB/VB.NET, PHP, Perl, SQL
Reply With Quote
  #3 (permalink)  
Old
Newbie Coder
 
Join Date: Oct 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Getting errors

Hi there... im getting errors with Users users = new Users();

Error 1
The type or namespace name 'Users' could not be found (are you missing a using directive or an assembly reference?)

Please kindly help me...
Reply With Quote
  #4 (permalink)  
Old
Newbie Coder
 
Join Date: Oct 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Cyberbratt View Post
Hi there... im getting errors with Users users = new Users();

Error 1
The type or namespace name 'Users' could not be found (are you missing a using directive or an assembly reference?)

Please kindly help me...
You didnot add a reference for Users class. (To use User class, add its reference)
Reply With Quote
  #5 (permalink)  
Old
Yeroon's Avatar
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 24 Times in 23 Posts
Or you didnt actually create the user class:

Code:
using System.Data;
using System.Data.SqlClient;

public class Users
{
    public bool AuthenticateUser(string username, string password)
    {
        bool authenticated;
        SqlConnection conn = new SqlConnection(Globals.ConnectionString());
        SqlCommand cmd = new SqlCommand("AuthenticateUser", conn);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@username", username);
        cmd.Parameters.AddWithValue("@password", password);
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();

        if (reader.Read())
        {
            authenticated = true;
        }
        else
        {
            authenticated = false;
        }

        reader.Close();
        conn.Close();
        conn.Dispose();

        return authenticated;
    } 
}
I replaced Add with AddWithValue, since the Add function is obsolete now. And I joined the "SqlDataReader reader" declaration and assigment.

Other than that this has been the most simple example I have seen for authentication
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #6 (permalink)  
Old
New Member
 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
"Globals does not exist in current context"

I have used this code and keep getting this error, "Globals does not exist in the current context." I've followed this code to the letter, could I be missing something?

Thanks so much!

~J
Reply With Quote
  #7 (permalink)  
Old
Yeroon's Avatar
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 24 Times in 23 Posts
Make sure you have a connection string in your web.config like this

Code:
<appSettings>
  <add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
And replace the line with the Globals error with this:

Code:
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionInfo"]);
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
login script required lochie Script Requests 2 03-06-04 07:44 PM
login script @NVP@ Script Requests 2 09-30-03 12:44 AM
help me improve my login script please paulj000 PHP 8 08-15-03 05:37 PM
merge into just one login ... ?? orgil PHP 4 07-30-03 05:06 PM
sitemember login tool pch Script Requests 5 07-08-03 12:28 PM


All times are GMT -5. The time now is 10:38 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.