Creating a Service with Visual Studio 2005

by Lord Cod3n 12. August 2008 03:20

Here is where I got the knowledge I need

http://aspalliance.com/1316_Working_with_Windows_Service_Using_Visual_Studio_2005

Safari OReilly ... I love this site, it saves me 100's of dollars a year by not having to purchase books.

http://safari.oreilly.com/

As promised in my previous post, here is some code you can use to impersonate in your application/service. 

I tested this in a service wrapper that was just garbage code, so I am only posting the impersonation class I converted.   One aside I would like to point out that I think is interesting is in a web.config, you can use an impersonate node to handle your permission issues with a different login.  With a windows application you have to explicitly call the win32 api’s.  I converted this from a c# example  found here . 

http://forums.asp.net/t/203769.aspx

 

Method Generation provided by  Really cool tool just found out about it

 http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120

 

Notice Default in the LoginProvider is called myDefault.  This is because Default is a reserved word in VB.  Since Enum are just number I felt safe making this change. 

'<summary>

'Provides methods for Identity impersonation.

' </summary>

Public Class Impersonation

'<summary>Used for the impersonation API calls.</summary>

Public Enum LogonType

Interactive = 2

Network

Batch

Service

End Enum

'<summary>Used for the impersonation API calls.</summary>

Public Enum LoginProvider

myDefault

WinNT35

End Enum

'<summary>Internal windows function used for loging in as a user</summary>

'<seealso cref="VerifyDomainPassword"/><seealso cref="BeginImpersonation"/><seealso cref="EndImpersonation"/>

'<param name="lpszUsername">The user name of the user to log in as</param>

'<param name="lpszDomain">The domain the user is in</param>

'<param name="lpszPassword">The users password</param>

'<param name="dwLogonType">Method of logging in. <see cref="LogonType"/></param>

' <param name="dwLogonProvider">Which provider to log in with. <see cref="LogonProvider"/></param>

'<param name="phToken">Token returned that points to the user. Needed for impersonation</param>

'<returns>False for success True for error</returns>

<System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="LogonUserA")> _

Public Shared Function LogonUser(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)> ByVal lpszUsername As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)> ByVal lpszDomain As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)> ByVal lpszPassword As String, ByVal dwLogonType As UInteger, ByVal dwLogonProvider As UInteger, ByRef phToken As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean

End Function

<System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="ImpersonateLoggedOnUser")> _

Public Shared Function ImpersonateLoggedOnUser(<System.Runtime.InteropServices.InAttribute()> ByVal hToken As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean

End Function

<System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="RevertToSelf")> _

Public Shared Function RevertToSelf() As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean

End Function

Public Shared Sub BeginImpersonation(ByVal UserId As String, ByVal Password As String, ByVal Domain As String)

Dim tokenHandle As IntPtr = IntPtr.Zero

Dim ret As Int32

Try

If Not (LogonUser(UserId, Domain, Password, LogonType.Interactive, LoginProvider.myDefault, tokenHandle)) Then

If Not (ImpersonateLoggedOnUser(tokenHandle)) Then

Throw New Exception("Could not impersonate")

End If

End If

Catch ex As Exception Throw New System.Exception("Problem Beginning Impersonation", ex)

End Try

End Sub

Public Shared Sub EndImpersonation()

Try

If Not (RevertToSelf()) Then

Throw New Exception("RevertToSelf;Failed")

End If

Catch ex As Exception Throw New Exception("Problem Ending Impersonation", ex)

End Try

End Sub

End Class

 

 

More to come in a bit , Code Happy

01100010011010010110110001101100

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

William Moore is the lead Software Architect and Technologist for Coden Enterprises. He has more than a decade of software development experience primarily Microsoft Platforms.  William enjoys the full gamit of coding everything from the UI down to the database.

Most comments

Page List