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"![Großes Lächeln](https://entwickler-forum.de/core/images/smilies/biggrin.png)
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
Des Weiteren die Klasse SampleIPrincipal
und abschließend die Verbindung mit der loginform1:
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
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"
![Großes Lächeln](https://entwickler-forum.de/core/images/smilies/biggrin.png)
Leider wird auf der MS-Hilfe aber nur eine Variante angeboten, die mit StoredPasswords arbeitet.
![Stirnrunzeln](https://entwickler-forum.de/core/images/smilies/frown.png)
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
Comment