p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   password encode sha1 all records (http://p2p.wrox.com/showthread.php?t=53079)

keyvanjan January 23rd, 2007 03:25 AM

password encode sha1 all records
 
hello,
i have a table with 200 records which has a password field
the passwords are not encoded
i need a page with a button when i click the button then
it goes and updates all the password fields to sha1 encoded
i have written some codes but it does not work
please write me the code

  Dim strPW As String = ""
        Dim dr As SqlDataReader



        Dim sql As String = "Select password from members_bkp"

        Dim cnn As New SqlClient.SqlConnection(strConnection)
        cnn.Open()

        Dim Cmd As New SqlCommand(Sql, cnn)

        dr = Cmd.ExecuteReader()
        dr.Read()
        Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfig File(dr("password"), "SHA1")
        dr.Close()
        Dim strSQL3 As String


        strSQL3 = "UPDATE members_bkp set password=@pwd"
        Cmd = New SqlCommand(strSQL3, cnn)
        Cmd.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))
        Cmd.ExecuteNonQuery()



gbianchi January 23rd, 2007 08:51 AM

hi there...

which part is not working??
if you want to updates all the fields to the same word why open a datareader before??
just the las part is ok...
Code:

    strSQL3 = "UPDATE members_bkp set password=@pwd"
    Cmd = New SqlCommand(strSQL3, cnn)
    Cmd.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))
    Cmd.ExecuteNonQuery()

also you say is not working... it's throw an error or is just not doing what is expected???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========

keyvanjan January 23rd, 2007 10:13 AM

no it updates all the passwords to 1 sha1 string
i want each password field to be converted to its own sha1


gbianchi January 23rd, 2007 11:33 AM

ok... and what part is not working?? because the logic of that function is no good...

you have to loop around all the records and update one at a time...

you still didn't tell us which part is no working...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========

keyvanjan January 23rd, 2007 11:50 AM

ok i really do not know
i tell you it works but all of the record get updated with one password
can you write the code so it loops and updates each record!!


gbianchi January 23rd, 2007 12:02 PM

you need to read the password for each record..

doing this
Code:

    dr.Read()
    Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfigFile(dr("password"), "SHA1")
    dr.Close()

will only read the password for the first record

you have to change your logic to something like this:

Code:

    Dim strPW As String = ""
    Dim dr As SqlDataReader
    Dim strSQL3 As String
    Dim sql As String = "Select password from members_bkp"
    Dim cnn As New SqlClient.SqlConnection(strConnection)
    cnn.Open()
    strSQL3 = "UPDATE members_bkp set password=@pwd"
    Cmd = New SqlCommand(strSQL3, cnn)
    Dim Cmd As New SqlCommand(Sql, cnn)
    dr = Cmd.ExecuteReader()
    do while dr.Read()
        Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfigFile(dr("password"), "SHA1")
        Cmd.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))
        Cmd.ExecuteNonQuery()
    end while
    dr.Close()

NOTE: the above will not work ok because the update command needs a where clause to tell it which record to update.. you will have to add it to the update command because I don't know your tables...






HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========

keyvanjan January 23rd, 2007 12:30 PM

i am really confused
what should i write in the parameters collection
i have this code

Dim strSQL3 As String
        Dim sql As String = "Select password from members_bkp"
        Dim cnn As New SqlClient.SqlConnection(strConnection)
        cnn.Open()
        Dim Cmd As New SqlCommand(sql, cnn)


        strSQL3 = "UPDATE members_bkp set password=@pwd"
        Cmd = New SqlCommand(strSQL3, cnn)


        Cmd.Parameters.Add(New SqlParameter("@pwd", dr("password")))
        dr = Cmd.ExecuteReader()
        While dr.Read()
            Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfig File(dr("password"), "SHA1")
            Cmd.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))
            Cmd.ExecuteNonQuery()
        End While
        dr.Close()




keyvanjan January 23rd, 2007 12:44 PM

I get error "there is already an open datareader"
on line :Cmd.ExecuteNonQuery()


 Dim strPW As String = ""
        Dim dr As SqlDataReader

        Dim cnn As New SqlClient.SqlConnection(strConnection)
        Dim sql As String = "Select password from members_bkp"
        Dim Cmd As New SqlCommand(Sql, cnn)




        cnn.Open()

        dr = Cmd.ExecuteReader()
        While dr.Read()
            Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfig File(dr("password"), "SHA1")

            Dim strSQL3 As String
            strSQL3 = "UPDATE members_bkp set password=@pwd"
            Cmd = New SqlCommand(strSQL3, cnn)
            Cmd.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))
            Cmd.ExecuteNonQuery()
        End While




gbianchi January 23rd, 2007 01:09 PM

well.. you cannot use a command for a reader and at the same time for a command...

you will need another command...


HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========

keyvanjan January 23rd, 2007 01:18 PM

I use the following code with a new command but i get this error:
There is already an open DataReader associated with this Command which must be closed first.

Dim cnn As New SqlClient.SqlConnection(strConnection)
        Dim sql As String = "Select password from members_bkp"
        Dim Cmd As New SqlCommand(sql, cnn)

        Dim strSQL3 As String

        cnn.Open()

        strSQL3 = "UPDATE members_bkp set password=@pwd"
        Dim Cmd2 As New SqlCommand(strSQL3, cnn)
        dr = Cmd.ExecuteReader()
        While dr.Read()
            Dim strPWEncoded As String = FormsAuthentication.HashPasswordForStoringInConfig File(dr("password"), "SHA1")

            Cmd2 = New SqlCommand(strSQL3, cnn)

            Cmd2.Parameters.Add(New SqlParameter("@pwd", strPWEncoded))

            Cmd2.ExecuteNonQuery()
        End While



All times are GMT -4. The time now is 07:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.