Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Snitz Forums 2000 MOD-Group
 MOD Add-On Forum (W/O Code)
 ASP.NET 2.0 Login Integration
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

the agony booth
Starting Member

19 Posts

Posted - 01 February 2007 :  17:30:34  Show Profile  Reply with Quote
Hi folks,

This is not exactly a MOD, but something related to the forums that I think other people might find useful.

I'm adding a new application to my site that's written in ASP.NET 2.0. I'd like existing users of the forum to be able to log in to this new application with their forum usernames and passwords. Basically, I don't want my users to have to create two separate logins for the same site.

Ideally, the ASP.NET 2.0 code would use FormsAuthentication, and then authenticate against the password in the FORUM_MEMBERS table.

The problem I'm running into is the SHA256 encryption. The .NET code needs to encrypt the user-entered password with SHA256 to do the comparison. I'm trying to use the System.Security.Cryptography.SHA256Managed class to do this, but it's giving me a completely different result than the function in inc_sha256.asp.

Has anyone tried this? I would really like to avoid rewriting inc_sha256.asp in .NET, unless I absolutely have to.

<

http://www.agonybooth.com/

the agony booth
Starting Member

19 Posts

Posted - 01 February 2007 :  17:47:38  Show Profile  Reply with Quote
Never mind. I found what I was looking for in this thread:
http://forum.snitz.com/forum/topic.asp?TOPIC_ID=63690

So that this topic isn't a total waste, I'll post my code once I get it working.

<

http://www.agonybooth.com/
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 01 February 2007 :  18:19:52  Show Profile  Visit HuwR's Homepage  Reply with Quote
I have fully integrated the Snitz forums with .Net 2 authentication and membership/profiles (see the Snitz .Net threads) if you want the code, I can post it for you.<
Go to Top of Page

the agony booth
Starting Member

19 Posts

Posted - 01 February 2007 :  18:31:06  Show Profile  Reply with Quote
Is that in the "ASP.NET Conversion" thread over in ASP.NET help? I figured it was somewhere in there, but it was a little tough to sift through all the posts to find it. If you can post the code here that would be great.

In the meantime, here's a little snippet I came up with. This actually might be all I need for my (very limited) requirements.

Login.aspx:


     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim oSqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("Forum").ConnectionString)

        Dim sSql As String = "select m.M_Password " & _
                                "from FORUM_MEMBERS m where m.M_Email = '" & UserEmail.Value & "'"

        Dim oSqlCmd As New SqlCommand(sSql, oSqlConn)
        oSqlConn.Open()

        Dim sPassword As String = oSqlCmd.ExecuteScalar()
        oSqlConn.Close()

        Dim sPassHash As String = SHA256Hash(UserPass.Value)
        If sPassHash.Equals(sPassword) Then
            FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, PersistCookie.Checked)
        Else
            Msg.Text = "Invalid Credentials: Please try again"
        End If

    End Sub


    Public Shared Function SHA256Hash(ByVal Data As String) As String
        Dim sha As SHA256 = New SHA256Managed
        Dim hash As Byte() = sha.ComputeHash(Encoding.ASCII.GetBytes(Data))
        Dim stringBuilder As StringBuilder = New StringBuilder
        For Each b As Byte In hash
            stringBuilder.AppendFormat("{0:x2}", b)
        Next
        Return stringBuilder.ToString
    End Function



EDIT: Big prize to whoever spots the potential SQL injection security hole first!


<

http://www.agonybooth.com/

Edited by - the agony booth on 01 February 2007 18:32:32
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 01 February 2007 :  19:42:24  Show Profile  Visit HuwR's Homepage  Reply with Quote
first off, you should be using a paramatized query if you want to avoid SQL injection problems

this is the code used by the Snitz .Net membership provider to validate a user (may be a little buggy as I normally write in C# not VB, so had to convert it.


Public Overloads Overrides Function ValidateUser(ByVal username As String, ByVal password As String) As Boolean 
 Dim sql As String = "Select MEMBER_ID From FORUM_MEMBERS WHERE M_NAME=@Username AND M_PASSWORD=@Password" 
 ' Using 
 Dim conn As SqlConnection = New SqlConnection(connStr) 
 Try 
   ' Using 
   Dim cmd As SqlCommand = New SqlCommand(sql, conn) 
   Try 
     Dim paramName As SqlParameter = New SqlParameter 
     paramName.ParameterName = "@Username" 
     paramName.Value = username 
     cmd.Parameters.Add(paramName) 
     Dim paramPwd As SqlParameter = New SqlParameter 
     paramPwd.ParameterName = "@Password" 
     paramPwd.Value = SHA256Hash(password) 
     cmd.Parameters.Add(paramPwd) 
     Try 
       conn.Open 
       Dim reader As SqlDataReader = cmd.ExecuteReader 
       If reader.HasRows Then 
         Return True 
       Else 
         Return False 
       End If 
     Catch 
       Return False 
     End Try 
   Finally 
     CType(cmd, IDisposable).Dispose() 
   End Try 
 Finally 
   CType(conn, IDisposable).Dispose() 
 End Try 
End Function

Public Shared Function SHA256Hash(ByVal Data As String) As String 
 Dim sha As SHA256 = New SHA256Managed 
 Dim hash As Byte() = sha.ComputeHash(Encoding.ASCII.GetBytes(Data)) 
 Dim stringBuilder As StringBuilder = New StringBuilder 
 For Each b As Byte In hash 
   stringBuilder.AppendFormat("{0:x2}", b) 
 Next 
 Return stringBuilder.ToString 
End Function


or if you want the C# version

	public static string SHA256Hash(string Data)
	{
        SHA256 sha = new SHA256Managed();
        byte[] hash = sha.ComputeHash( Encoding.ASCII.GetBytes(Data) );

        StringBuilder stringBuilder = new StringBuilder();
        foreach( byte b in hash ) 
        {
            stringBuilder.AppendFormat("{0:x2}", b);
        }
        return stringBuilder.ToString();
    }

	public static bool ValidateUser(string username, string password)
	{
        string sql = "Select MEMBER_ID From FORUM_MEMBERS WHERE M_NAME=@Username AND M_PASSWORD=@Password";

        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                SqlParameter paramName = new SqlParameter();
                paramName.ParameterName = "@Username";
                paramName.Value = username;
                cmd.Parameters.Add(paramName);
                SqlParameter paramPwd = new SqlParameter();
                paramPwd.ParameterName = "@Password";
                paramPwd.Value = SHA256Hash(password);
                cmd.Parameters.Add(paramPwd);

                try
                {
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch                 {
                    //throw new Exception("Login Error");
                    return false;
                }
            }
        }
	}
<
Go to Top of Page

puthen
Starting Member

7 Posts

Posted - 02 October 2007 :  18:11:22  Show Profile  Reply with Quote
Hi,
Did the agony booth get it to work?.If so can you post the code ?
puthen<

This account was hacked into by Image, a very honest guy as you all can see! Stealing people' s passwords IS his pasttime.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.47 seconds. Powered By: Snitz Forums 2000 Version 3.4.07