Solved

using application roles in vb

Posted on 2002-03-14
8
863 Views
Last Modified: 2008-02-01
hi

i am trying to use vb as my front end and sql server as the back end db. I want to use application roles.

I understand that after executing sp_setapprole the users permissions are ignored and the permissions of the application role are used instead.

I have a table called employee where the username 'ms' has no select permissions but the app role 'SalesApp' has select permissions.

Everything is fine until i try to select. How can i select as 'SalesApp' and not as 'ms'. Below is my code:



Private Sub cmdOK_Click()
    Dim Cmd As New ADODB.Command, cmdApplication As New ADODB.Command
    Dim rs As New ADODB.Recordset, employeeRs As ADODB.Recordset, LogRs As ADODB.Recordset
    Dim ApplicationRs As ADODB.Recordset
    Dim param1 As Parameter
    Dim paramApplicationReturn As Parameter, paramApplication1 As Parameter, paramApplication2 As Parameter
    Dim connectString As String
    Dim blnFound As Boolean, blnPasswordChange As Boolean
    Dim strCriteria As String

    Dim strEmployeeCriteria As String, strMessage As String, strTempUsername As String
    Dim i As Integer

    'Add your startup procedures here.
    About.Visible = False
    About.cmdOK.Visible = False
    About.Show
    About.Refresh


    Screen.MousePointer = vbHourglass
    Set conn = New ADODB.Connection
   
    On Error GoTo LoginFailed:
   
   
         
    conn.ConnectionString = "Provider=SQLOLEDB;Data Source=Temp;" _
         & "User ID=ms;Password=" & _
         ";Initial Catalog=HelpdeskCL; ConnectionTimeout = 60"
    conn.Open
   
    If (conn.State = adStateOpen) Then
   
        cmdApplication.CommandText = "sp_setapprole"
        cmdApplication.CommandType = adCmdStoredProc
       
        Set paramApplicationReturn = cmdApplication.CreateParameter("Return", adInteger, adParamReturnValue)
        cmdApplication.Parameters.Append paramApplicationReturn
       
        Set paramApplication1 = cmdApplication.CreateParameter("@strUsername", adChar, adParamInput, 20)
        cmdApplication.Parameters.Append paramApplication1
        cmdApplication.Parameters(1).Value = "SalesApp"
       
       
        Set paramApplication2 = cmdApplication.CreateParameter("@strPsd", adChar, adParamInput, 20)
        cmdApplication.Parameters.Append paramApplication2
        cmdApplication.Parameters(2).Value = "SalesTest"
       
        cmdApplication.ActiveConnection = conn
        Set ApplicationRs = cmdApplication.Execute


    Else
        MsgBox ("Failed Connecting")
        Exit Sub
    End If


    strCriteria = "select * From employee where username =" & Trim(txtUsername.Text)
    rs.Open strCriteria, conn, adOpenDynamic, adLockOptimistic, adCmdText

    MDIForm1.Show
   

LoginFailed:
    If paramApplicationReturn = 1 Then
        strMessage = MsgBox("You have failed to log in!  ", vbExclamation)
    Else
        strMessage = MsgBox(Err.Number & "   " & Err.Description, vbExclamation)
        conn.RollbackTrans
    End If

    Screen.MousePointer = vbArrow
    Unload Me
'Unload MDIForm1
End Sub


Rg,
Mehul
0
Comment
Question by:melu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 6864351
Your problem could be the following line:
cmdApplication.ActiveConnection = conn

replace it by:
set cmdApplication.ActiveConnection = conn

The difference is that the first line will pass the connection string to the command object, which will open a "new" connection. The setapprole will be executed on that connection, and get lost as soon as this line finishes. The original connection object didn't get the new permission settings.

The second line will pass the connection object to the command, and use that one. The connection object WILL have the new permissions.

CHeers

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6864363
angelIII (or anyone else), what are the benefits of using application roles?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6864386
BOL:
If ad hoc access to a database is not required, users and Windows NT 4.0 or Windows 2000 groups do not need to be granted any permissions because all permissions can be assigned by the applications they use to access the database. In such an environment, standardizing on one system-wide password assigned to an application role is possible, assuming access to the applications is secure

The main difference is that everybody could connect in general to the database but not being able to do anything. Using the application role, the application tasks can be done, but the user shoulnd't be able to use QA to do things "manually".

CHeers
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6864390
... of course, using SP can do the same things also, but if you don't want to use SP, and still have a secure database, then you use Application Roles.

CHeers
0
 

Author Comment

by:melu
ID: 6870543
Hi All,

To angelIII - no that doesn't seem to work. What i mean that
(refering to the code)

   cmdApplication.ActiveConnection = conn
   Set ApplicationRs = cmdApplication.Execute

executes fine.

The next line

   strCriteria = "select * From employee where username =" & Trim(txtUsername.Text)
   rs.Open strCriteria, conn, adOpenDynamic, adLockOptimistic, adCmdText

i.e rs.Open gives an error because the user 'ms' has no select permissioms on the
employees table but the appilcation role 'SalesApp' has select permissions on this
table.

How can i open this table as the app role?

Thanks,
Rg
Mehul
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6876149
Do you have a "deny select" for the guest user on that table? I tested your code on my system (obviously changing names and passwords), and all worked fine unless I had a deny select on the table for the user guest. I could have a deny on the user ms...
CHeers
0
 

Author Comment

by:melu
ID: 6876345
No i dont - just that i have not checked the select permissions of user ms (i.e its neutral).

i mean it works b/c if you view the properties of the conn object - its using ms loginname and password etc.

i want it to use:

rs.Open strCriteria, cmdApplication, adOpenDynamic,adLockOptimistic, adCmdText

i.e use the Application Roles security context.

Rg,
Mehul
0
 

Author Comment

by:melu
ID: 6917370

It works fine now. I think this line of code does it for me:

    strCriteria = "sp_setapprole 'SalesApp', 'SalesTest'"
    conn.Execute strCriteria

so even when user ms has no select permissions on the employees table
the appl is able to select using the Appl Role 'SalesApp'.

   

    conn.ConnectionString = "Provider=SQLOLEDB;Data Source=Temp; OLE DB Services= -2;" _
         & "User ID=ms;Password=" & _
         ";Initial Catalog=HelpdeskCL; ConnectionTimeout = 60"
    conn.Open
   

   
   
    If (conn.State = adStateOpen) Then
   
        ' Option 3
        ' Setting the Application Role
        strCriteria = "sp_setapprole 'SalesApp', 'SalesTest'"
        conn.Execute strCriteria

    Else
        GoTo LoginFailed
    End If

   

    Set employeeRs = New ADODB.Recordset
   
    strEmployeeCriteria = "SELECT * FROM employee WHERE UserName= '" & Trim(txtUsername.Text) & "'" & _
                          "AND [Password]= '" & Trim(txtPassword.Text) & "'" & " AND [Active] = 1"
    employeeRs.Open strEmployeeCriteria, conn, adOpenDynamic, adLockOptimistic, adCmdText


Rg,
Mehul
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question