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.
https://www.experts-exchange.com/questions/29062577/I-need-to-perform-user-impersonation-using-VB-net-to-a-local-user-account-on-a-remote-computer.html
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)
Try
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
CloseHandle(tokenHandle)
Else
'logon failed
End If
Catch ex As Exception
'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:
https://www.mindstick.com/Articles/478/dynamically-loading-image-in-image-control-in-asp-dot-net
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”:
https://forums.asp.net/t/507202.aspx?can+I+load+image+from+URL
Once that was completed I discovered that a Tiff could contain more than one page. Then I found and implemented the below:
https://www.ryadel.com/en/multipage-tiff-files-asp-net-c-sharp-gdi-alternative/
Fianlly I stiched the pages into one image usign the solution below from the user “Pascalsz”
https://stackoverflow.com/questions/7206510/combine-two-images-into-one-new-image?lq=1
Below is a helpful tip on how to get the library:
http://www.dreamincode.net/forums/topic/131847-trying-to-get-using-systemwindowsmediaimaging%3B-to-work/
Read Full Post »