Search code examples
asp.netif-statementauthenticationpasswordslogin-script

Multiple Username and Password accounts


I have the following code to log onto a separate Admin area within my site:

<script runat="server">

Public Sub Login(ByVal s As Object, ByVal e As EventArgs)

If UserName.Text = "admin" And Password.Text = "pw11" Then
Session("Admin") = True
Response.Redirect("default.aspx")

Else

Session("Admin") = False
LtlLogin.Text = "<p>Sorry you have provided incorrect login details.</p>"

End If
End Sub

</script>

How can I have it so it has another two or three username and password accounts to log on?

Thanks in advance!


Solution

  • Private ReadOnly _credentials = New Dictionary(Of String, String) From {
        {"admin", "pw11"},
        {"admin1", "pw12"},
        {"admin2", "pw13"}
    }
    
    Public Sub Login(ByVal s As Object, ByVal e As EventArgs)
        Dim storedPassword = ""
        If _credentials.TryGetValue(UserName.Text, storedPassword) And storedPassword = Password.Text Then
            Session("Admin") = True
            Response.Redirect("default.aspx")
        End If
    
        Session("Admin") = False
        LtlLogin.Text = "<p>Sorry you have provided incorrect login details.</p>"
    End Sub