rommelit
asked on
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()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you show me the code of your GenericIdentity class?
ASKER
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
Are you sure?
ASKER
Oh no. Thats just to check the authentication.
Isnt is just this?
Dim gIdentity As New GenericIdentity(txtUsernam e.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()
Isnt is just this?
Dim gIdentity As New GenericIdentity(txtUsernam
Dim sArray(0 To 1) As String
sArray(0) = "Manager"
sArray(1) = "Engineer"
Dim uPrincial As New GenericPrincipal(gIdentity
Thread.CurrentPrincipal = uPrincial
'frmConfig.Show()
You are instantiating gIdentity as an instance of GenericIdentity. I am talking about the code of GenericIdentity class.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
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.
Its a personal preference for me. Its useful to have all the info in one object.
ASKER
So I think I am going to stick with the Generic Principal approach but awarding points given that Code's approach is perfectly valid.
ASKER
Validated
ASKER
Here is a good article for anyone who finds this is EE http://www.codeproject.com/KB/security/SecurityModelDotNet.aspx