?
Solved

Connecting to SQL 2005 database using Domain Account

Posted on 2010-09-16
4
Medium Priority
?
310 Views
Last Modified: 2012-08-13
I am trying to connect to a database on SQL 2005 using the only thing that has perms to it right now -- a windows account.   It's a service account though, not the IUSR account on the web server.  Anyone have an example of a working ASP script to connect to a SQL 2005 database with a domain service account?

Thanks in advance,
TWB
0
Comment
Question by:TWB_Systems
[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
  • 2
4 Comments
 
LVL 15

Expert Comment

by:AmmarR
ID: 33697324
for your particular site, change the IIS settings, and make it instead of anonymous access authentication method, change it to Integrated windows authentication.

or if you cant do that

you can login with your code as a different windows user and access sql.

check code below



public sub connecttosql()
        If impersonateValidUser('validuser', _
        'validdomain', 'validpassword') Then
            'Insert your code that runs under the security context of a specific user here.
            ' insert the code to connect to sql under this user
            undoImpersonation()
        Else
            'Your impersonation failed. Therefore, include a fail-safe mechanism here.
        End If
end sub


    Public Function impersonateValidUser(ByVal userName As String, _
    ByVal domain As String, ByVal password As String) As Boolean

        Dim tempWindowsIdentity As WindowsIdentity
        Dim token As IntPtr = IntPtr.Zero
        Dim tokenDuplicate As IntPtr = IntPtr.Zero
        impersonateValidUser = False

        If RevertToSelf() Then
            If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
                If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                    tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
                    impersonationContext = tempWindowsIdentity.Impersonate()
                    If Not impersonationContext Is Nothing Then
                        impersonateValidUser = True
                    End If
                End If
            End If
        End If
        If Not tokenDuplicate.Equals(IntPtr.Zero) Then
            CloseHandle(tokenDuplicate)
        End If
        If Not token.Equals(IntPtr.Zero) Then
            CloseHandle(token)
        End If
    End Function

Open in new window

0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33698763
Please check the following link which contains all the options for connecting to SQL server 2005
http://www.connectionstrings.com/sql-server-2005
0
 
LVL 22

Accepted Solution

by:
Om Prakash earned 2000 total points
ID: 33698771
Example:

dim conn
strConn = "Driver={SQL Server};Server=Server_name;Database=database_name;Trusted_Connection=Yes;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString= strConn
conn.Open

Open in new window

0
 

Author Closing Comment

by:TWB_Systems
ID: 33737170
Not the route I wanted to go, but the route I ended up taking.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 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