Posts Tagged ‘vb.net’

I needed a way to access a shared location that prompted for user name and password adn downloaded/diaplyed a TIFF image.
I had the credentials with me but to give the users a seemless experience I had to embbed that username and password.

I tired the impersonation from the link below but it generated “An error occurred while attempting to impersonate.” No matter what I tried it did not work. It couold that there was a policy that didn’t allow impersonation.


Then from vairous different research I figures a way of just. Using the below code I could pass the username and password to access the location.

    Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal un As String, ByVal domain As String, ByVal pw As String, ByVal LogonType As Integer, ByVal LogonProvider As Integer, ByRef Token As IntPtr) As Boolean
    Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

  Dim tokenHandle As New IntPtr(0)

            If LogonUser("username", "domain", "password", 2, 0, tokenHandle) Then
                Dim newId As New WindowsIdentity(tokenHandle)
                Using impersonatedUser As WindowsImpersonationContext = newId.Impersonate()

			'perform impersonated commands

                End Using
                'logon failed

            End If
        Catch ex As Exception

        End Try

Now I needed to display the image in an asp.net Image control. I soon found that the control cannot read TIFF directly so I used the below technique:


But then I realised there was more restriction because copying the file over using File.Copy was locking the file until I closed the window. I realised I could may be stream the image in the browser since the files can be accessed from the IIS server directly.
I used the below technique from the user “afetchko”:


Once that was completed I discovered that a Tiff could contain more than one page. Then I found and implemented the below:


Fianlly I stiched the pages into one image usign the solution below from the user “Pascalsz”


Below is a helpful tip on how to get the library:


Read Full Post »