Passing in role from login

I currently have a hard coded array with a couple of roles.  I want to substitue the role from a table in my SQL db.  How can I pull it back and persist the role through the appplication?
        Try
            conn.ConnectionString = setting.SERVIRENT_ConnectionString
            cmd.CommandText = "SELECT * FROM [Employees] WHERE Username= '" & txtUsername.Text & "'AND Password='" & txtPassword.Text & "';"
            conn.Open()
            cmd.Connection = conn
            adapt.SelectCommand = cmd
            adapt.Fill(ds, "0")

            Dim count = ds.Tables(0).Rows.Count
            If count > 0 Then
                setting.server = txtUsername.Text
                setting.Save()
                Dim gIdentity As New GenericIdentity(txtUsername.Text)
                Dim sArray(0 To 1) As String
                sArray(0) = "Admin"
                sArray(1) = "Limited"
                Dim uPrincial As New GenericPrincipal(gIdentity, sArray)

                Thread.CurrentPrincipal = uPrincial
                'frmConfig.Show()
            Else
                MsgBox("Invalid Login")
                'Me.Close()
                'frmLaunchpad.Show()
            End If
            conn.Close()
            'Me.Close()

Open in new window

rommelitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
You can make a User class like below


With this class, I think you wont need to use generic identity class. You can then populate this class with the above code and with a datareader which loops through the roles table in your db and adds roles to the List(of String). Let me know if you need help with that


Public Class User
   Private _UserID As Guid
   Public Sub New()
     _UserID = New Guid()
   End Sub
   Public Property UserName As String
   Public Property Roles As List(of String)
   Public ReadOnly Property UserID As String
   Get
      Return _UserID.Tostring()
   End Get
   ...
End Class

Open in new window

0
CodeCruiserCommented:
Can you show me the code of your GenericIdentity class?
0
rommelitAuthor Commented:
Here it is
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cPrincipal As GenericPrincipal
        cPrincipal = Thread.CurrentPrincipal
        If cPrincipal.Identity.IsAuthenticated And cPrincipal.IsInRole("Manager") Then

            Label1.Text = "You are authenticated"
        Else
            Label1.Text = "No go"
        End If
    End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CodeCruiserCommented:
Are you sure?
0
rommelitAuthor Commented:
Oh no.  Thats just to check the authentication.

Isnt is just this?
                Dim gIdentity As New GenericIdentity(txtUsername.Text)
                Dim sArray(0 To 1) As String
                sArray(0) = "Manager"
                sArray(1) = "Engineer"
                Dim uPrincial As New GenericPrincipal(gIdentity, sArray)

                Thread.CurrentPrincipal = uPrincial
                'frmConfig.Show()
0
CodeCruiserCommented:
You are instantiating gIdentity as an instance of GenericIdentity. I am talking about the code of GenericIdentity class.
0
rommelitAuthor Commented:
Just importing system.security.principal.  I did not write a class for GenericIdentity.  is this wrong?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
No no. There is another question about GenericIdentity class so I thought your code may help that chap. Have you tried my idea of a User class?
0
rommelitAuthor Commented:
Gonna try it as it seems like a reasonable course of action.  I am just curious why one would pick one over the other.  That's all.  Maybe it's just a case of different strokes for different folks.
0
CodeCruiserCommented:
Its a personal preference for me. Its useful to have all the info in one object.
0
rommelitAuthor Commented:
So I think I am going to stick with the Generic Principal approach but awarding points given that Code's approach is perfectly valid.
0
rommelitAuthor Commented:
Validated
0
rommelitAuthor Commented:
Here is a good article for anyone who finds this is EE http://www.codeproject.com/KB/security/SecurityModelDotNet.aspx 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.