Solved

using application roles in vb

Posted on 2002-03-14
8
856 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
  • 4
  • 3
8 Comments
 
LVL 142

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 142

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 142

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 142

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

778 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