Announcement

Collapse
No announcement yet.

Hashpasswort erzeugen und wieder auslesen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hashpasswort erzeugen und wieder auslesen

    Hallo Leute,

    habe ein riesen Problem. In meiner kleinen Datenbankanwendung möchte ich eine Anmeldung einfügen, die über den Abgleich gehashter Passwörter die Authentifizierung vornehmen soll.
    Ich benutze hierfür zwei Klassen Namens SampleIIdentety und SampleIprinzipal (dürfte einigen denke ich bekannt sein).Das Prinzip finet jeder in der MS-Hilfe wieder unter Suchbegriff: "Authentifizierung"

    Leider wird auf der MS-Hilfe aber nur eine Variante angeboten, die mit StoredPasswords arbeitet. Ich strebe aber die Speicherung in meiner Datenbank an.

    Ich habe Tage verbracht, um das Problem zu lösen, es aber nicht hinbekommen. Zudem kommt der wichtige gedanke, wie ich neue Hashpasswörter erzeuge, wenn ein neuer User angemeldet werden soll.



    Und hier einmal der Code (von MS):

    Als erstes die Klasse SampleIIdentety

    Code:
    Public Class SampleIIdentity
        Implements System.Security.Principal.IIdentity
    
        Private nameValue As String
        Private authenticatedValue As Boolean
        Private roleValue As ApplicationServices.BuiltInRole
    
        Public ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.AuthenticationType
            Get
                Return "Custom Authentication"
            End Get
        End Property
    
        Public ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticated
            Get
                Return authenticatedValue
            End Get
        End Property
    
        Public ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Name
            Get
                Return nameValue
            End Get
        End Property
    
        Public ReadOnly Property Role() As ApplicationServices.BuiltInRole
            Get
                Return roleValue
            End Get
        End Property
    
        Public Sub New(ByVal name As String, ByVal password As String)
            ' The name is not case sensitive, but the password is.
            If IsValidNameAndPassword(name, password) Then
                nameValue = name
                authenticatedValue = True
                roleValue = ApplicationServices.BuiltInRole.Administrator
            Else
                nameValue = ""
                authenticatedValue = False
                roleValue = ApplicationServices.BuiltInRole.Guest
            End If
        End Sub
    
        Private Function IsValidNameAndPassword( _
            ByVal username As String, _
            ByVal password As String) _
            As Boolean
    
            ' Look up the stored hashed password and salt for the username.
            Dim storedHashedPW As String = GetHashedPassword(username)
            Dim salt As String = GetSalt(username)
    
            'Create the salted hash.
            Dim rawSalted As String = salt & Trim(password)
            Dim saltedPwBytes() As Byte = _
                System.Text.Encoding.Unicode.GetBytes(rawSalted)
            Dim sha1 As New _
                System.Security.Cryptography.SHA1CryptoServiceProvider
            Dim hashedPwBytes() As Byte = sha1.ComputeHash(saltedPwBytes)
            Dim hashedPw As String = Convert.ToBase64String(hashedPwBytes)
    
            ' Compare the hashed password with the stored password.
            Return hashedPw = storedHashedPW
        End Function
    
        Private Function GetHashedPassword(ByVal username As String) As String
            ' Code that gets the user's hashed password goes here.
            ' This example uses a hard-coded hashed passcode.
            ' In general, the hashed passcode should be stored 
            ' outside of the application.
            If Trim(username).ToLower = "testuser" Then
                Return "ZFFzgfsGjgtmExzWBRmZI5S4w6o="
            Else
                Return ""
            End If    End Function
    
        Private Function GetSalt(ByVal username As String) As String
            ' Code that gets the user's salt goes here.
            ' This example uses a hard-coded salt.
            ' In general, the salt should be stored 
            ' outside of the application.
            If Trim(username).ToLower = "testuser" Then
                Return "Should be a different random value for each user"
            Else
                Return ""
            End If 
    End Function
    
    End Class

    Des Weiteren die Klasse SampleIPrincipal

    Code:
    Public Class SampleIPrincipal
        Implements System.Security.Principal.IPrincipal
    
        Private identityValue As SampleIIdentity
    
        Public ReadOnly Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identity
            Get
                Return identityValue
            End Get
        End Property
    
        Public Function IsInRole(ByVal role As String) As Boolean Implements System.Security.Principal.IPrincipal.IsInRole
            Return role = identityValue.Role.ToString
        End Function
    
        Public Sub New(ByVal name As String, ByVal password As String)
            identityValue = New SampleIIdentity(name, password)
        End Sub
    
    End Class

    und abschließend die Verbindung mit der loginform1:


    Code:
    Dim samplePrincipal As New SampleIPrincipal( _
        Me.UsernameTextBox.Text, Me.PasswordTextBox.Text)
    Me.PasswordTextBox.Text = ""
    If (Not samplePrincipal.Identity.IsAuthenticated) Then
        ' The user is still not validated.
        MsgBox("The username and password pair is incorrect")
    Else
        ' Update the current principal.
        My.User.CurrentPrincipal = samplePrincipal
        Me.Close()
    End If


    Könnt Ihr mir helfen, wie ich den Code so umschreibe, dass die Benutzerdaten aus der Datenbank gelesen werden?

    Ich danke euch sehr für Eure Hilfe.

    Eure Laura

  • #2
    Hallo Laura,

    Ein moeglicher Ansatz waere, dass du das gehashte Passwort in der DB speicherst und beim Login nur noch den gehashten Wert in der DB mit dem eingegebenen (gehashten) Passwort vergleichst.

    Vorteil: Das Passwort kann nicht aus der DB ausgelesen / ueber das Netzwerk gesnifft werden.
    Nachteil: Vergisst der User das Passwort, kann man es nicht in der DB nachlesen - wirklich ein Nachteil?

    Dann waere deine Verifizierung des Passworts auch nicht sooo schwierig - ein Beispiel:
    - das eingegebene Passwort hashen
    - eine Verbindung zur DB aufbauen
    - einen Command, wie SELECT COUNT(*) FROM usertable WHERE username = 'username' AND password = hashwert, zB mit ExecuteScalar absetzen
    - kommt ein Wert groesser 0 zurueck sind die User-Credentials gueltig

    Hilft dir das weiter?
    *-- robert.oh. --*

    Comment


    • #3
      Hey Robert,

      habs gleich mal ausprobiert, es klappt wie soll ich sagen, PERFEKT.

      Du hast mich vor weiteren schlaflosen nächten gerettet.
      Vielen lieben Dank dafür.

      Bis bald mal hoffe ich

      Laura

      Comment


      • #4
        Na da bin ich aber froh, dass du wieder ruhig schlafen kannst :-P
        Freut mich, wenn ich dir helfen konnte.

        Schoenen Abend und eine angenehme Nachtruhe
        *-- robert.oh. --*

        Comment

        Working...
        X