473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Login Validation

11
Hi All ,

I am working with Asp.net 2.0 C# and my database is SQl 2005 .I hve a problem with putting Login Validation . I want tht when user enters username and Password it should be verified with database if ID PWD matches then user should be logged in and if it doesn't match then it should give login failed . Can u plz pass on d code if possible .

Thanks in advance
Mar 9 '07 #1
3 2796
Frinavale
9,735 Expert Mod 8TB
Hi All ,

I am working with Asp.net 2.0 C# and my database is SQl 2005 .I hve a problem with putting Login Validation . I want tht when user enters username and Password it should be verified with database if ID PWD matches then user should be logged in and if it doesn't match then it should give login failed . Can u plz pass on d code if possible .

Thanks in advance
What I've done in the past is store the user names (or ID's) and password hashes & salt values (never store passwords) in the database. Then when the user supplies their credentials, you select from the database, check if the password hash values match and compare the information.

-Frinny
Mar 9 '07 #2
kiran01
11
Can u elaborate on the hash value part u said ???? should i write query for each n every username , so tht password is in hash value format .
Mar 12 '07 #3
Frinavale
9,735 Expert Mod 8TB
Can u elaborate on the hash value part u said ???? should i write query for each n every username , so tht password is in hash value format .
Well its not that hard.

You create a "salt" value that is used to randomize the hash values you'll create for each person. You'll need to use System.Security.Cryptography to create the salt and then later System.Web.Security to create the hash.

My examples are going to be written in VB but I'm sure they can easily be converted into C#. Also please note that there might be newer methods available to do this because I wrote this a few years ago.

Anyways, to create the salt:
Expand|Select|Wrap|Line Numbers
  1. Private Function CreateSalt(ByVal numBytes As Integer) As String
  2.      Dim rng As New  RNGCryptoServiceProvider 
  3.      Dim buff() As Byte = New Byte(numBytes){}
  4.      rng.GetBytes(buff)
  5.      Return Convert.ToBase64String(buff)
  6. End Function
  7.  
Now that you have the salt you can create a Hash of the password.

You take the user's password and salt value and use the FormsAuthentication to create a Hash value from these two values.

You can place your salt value at any spot in the password to make it harder to crack. Now, if someone does get a hold of your database they'll have a bunch of hash values and salt values but wont have any passwords.

Even if they have obtained these values and start trying to recreate the hash values you have stored, they don't know where the salt has been added to the password. It'll make it even harder for them to get the password.

Also, since the salt value is random no two hash values will be the same. This means that if two people have the password "password", their hash values will be different so glancing at the hash values stored in your database wont reveal anything.

Please note that you cannot "unhash" a password. Its a one way thing. You can hash something but cannot derive the password from the hash.

To create the hash you use a function that Microsoft was kind enough to provide to us:
Expand|Select|Wrap|Line Numbers
  1. Public Function CreatePasswordHash(ByVal password As String, ByVal salt as String) As String
  2.      Return FormsAuthenticiation.HashPasswordForStoringInConfigFile(password & salt, "SHA1")
  3. End Function
  4.  
This example will create a hash for you (for storing) using SHA1 encryption.

You store the Hash and the Salt values into the database.

Now when your user comes back to the page you do a query on their userName and retrieve their Hash and Salt values from the database. Then you recreate the hash value by taking the password they supplied and passing it to the same encryption method as you used to originally create the password Hash. You compare the newly created hash value to the one you retrieved from the database to determine if their credentials are correct.

Hope this helps!

-Frinny
Mar 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mo | last post by:
The code I've pasted below is taken directly from Microsoft's site at http://support.microsoft.com/default.aspx?scid=kb;EN-US;308157 As far as I can tell the error is raised on this line: conn...
2
by: Lloyd Dupont | last post by:
I wan to use the Login control but I want to do the login myself (use the 'login' text as either the email or the usewr login) how could I do that?
4
by: Pawel Janik | last post by:
Hello! I was recently worked on building quite complicated web app. Everything was ok, it was quite good tested using most popular browsers (IE6.0, Firefox/Mozilla, Opera). I have problem with...
3
by: Jon Paal | last post by:
using VWD and asp.net 2.0 how do I enable clientside validation in ligin control ? Following code does not produce popup alerts or red asterisks . ==================== <div id="content">...
3
by: ilockett | last post by:
The background: I have a web app with a simple master page that contains just one content placeholder. I have created a web form that then uses this master page. Within the content...
2
by: Vai2000 | last post by:
Hi All, I have a website which allows users to visit anonymously. Throughout the page I offer a signup link thru which a user can create an account. I want the user to be redirected to the page on...
5
by: rockdale | last post by:
Hi, all: I have a website with its own login page. Now one of my clients want their employees log into my website from their website. They want to have their login page (look and feel are...
2
by: wqmmnm | last post by:
I am trying to develop a PHP site so that I may record my poker winnings and display the results on my blog. I haven't used php in a long time so I need to beginner type help. I know I am going...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
0
by: daokfella | last post by:
I have a Login.aspx page that takes care of all my login procedures (validation, lockouts, password change requirements, password retrieval, etc.) It works like a charm. However, now I'd like a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.