Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Reading PDF Files on Remote Server [modified]

Reading PDF Files on Remote Server [modified]

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netsysadminwindows-adminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bgates1970
    wrote on last edited by
    #1

    I know this is sort of an old topic but with all the code snippets I've attempted I still can't get it to perform consistently. Essentially, I'm trying to open a PDF on a server that is not on the domain but a Workgroup. The IIS 6 server is on the domain and the files reside on the Workgroup server. I'm using VS2008/.NET 3.5 *** Code to Open File from ASP.NET page '\\Server1\contracts\newlondon.pdf

    Dim sFullPath As String = sServer + sFolder + sFile

    Dim wcClient As WebClient = New WebClient()

    Dim obj As UserManager = New UserManager
    obj.CreateIdentity("user1", "Server1", "password")

    Dim buffer As Byte() = wcClient.DownloadData(sFullPath)

    Response.ClearContent()
    Response.AddHeader("Content-Disposition", "inline; filename=userdoc" + oDocument.FileExtension.ToString)
    Response.ContentType = returnExtension(oDocument.ContentType.ToString)
    Response.AddHeader("content-length", buffer.Length.ToString)
    Response.BinaryWrite(buffer)

    obj = Nothing

    *** *** Create Identity (which I found on CodeProject)

    Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _
    ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, _
    ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer

    Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
    
    Public Sub CreateIdentity(ByVal User As String, ByVal Domain As String, ByVal Password As String)
        Dim tokenHandle As New IntPtr(0)
        Dim ret As Integer
        Dim LOGON32\_PROVIDER\_DEFAULT As Integer = 0
        Dim LOGON32\_LOGON\_NETWORK\_CLEARTEXT As Integer = 3
    
        tokenHandle = IntPtr.Zero
        Dim returnValue As Boolean = LogonUser(User, Domain, Password, LOGON32\_LOGON\_NETWORK\_CLEARTEXT, LOGON32\_PROVIDER\_DEFAULT, tokenHandle)
        If False = returnValue Then
            ret = Marshal.GetLastWin32Error()
            Throw New Exception("LogonUser failed with error code: " + ret.ToString)
        End If
        Dim id As New WindowsIdentity(tokenHandle)
        CloseHandle(tokenHandle)
        id.Impersonate()
    End Sub
    

    ***** I've had it working a couple times on my workstation but never from the webserver. I usually get "Unknown username or password" or "Access to ..... denied!" I have identity impersonate="true" in the Web.Config.

    modified on Thursday, November 11, 2010 10:55 AM

    N 1 Reply Last reply
    0
    • B bgates1970

      I know this is sort of an old topic but with all the code snippets I've attempted I still can't get it to perform consistently. Essentially, I'm trying to open a PDF on a server that is not on the domain but a Workgroup. The IIS 6 server is on the domain and the files reside on the Workgroup server. I'm using VS2008/.NET 3.5 *** Code to Open File from ASP.NET page '\\Server1\contracts\newlondon.pdf

      Dim sFullPath As String = sServer + sFolder + sFile

      Dim wcClient As WebClient = New WebClient()

      Dim obj As UserManager = New UserManager
      obj.CreateIdentity("user1", "Server1", "password")

      Dim buffer As Byte() = wcClient.DownloadData(sFullPath)

      Response.ClearContent()
      Response.AddHeader("Content-Disposition", "inline; filename=userdoc" + oDocument.FileExtension.ToString)
      Response.ContentType = returnExtension(oDocument.ContentType.ToString)
      Response.AddHeader("content-length", buffer.Length.ToString)
      Response.BinaryWrite(buffer)

      obj = Nothing

      *** *** Create Identity (which I found on CodeProject)

      Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _
      ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, _
      ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer

      Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
      
      Public Sub CreateIdentity(ByVal User As String, ByVal Domain As String, ByVal Password As String)
          Dim tokenHandle As New IntPtr(0)
          Dim ret As Integer
          Dim LOGON32\_PROVIDER\_DEFAULT As Integer = 0
          Dim LOGON32\_LOGON\_NETWORK\_CLEARTEXT As Integer = 3
      
          tokenHandle = IntPtr.Zero
          Dim returnValue As Boolean = LogonUser(User, Domain, Password, LOGON32\_LOGON\_NETWORK\_CLEARTEXT, LOGON32\_PROVIDER\_DEFAULT, tokenHandle)
          If False = returnValue Then
              ret = Marshal.GetLastWin32Error()
              Throw New Exception("LogonUser failed with error code: " + ret.ToString)
          End If
          Dim id As New WindowsIdentity(tokenHandle)
          CloseHandle(tokenHandle)
          id.Impersonate()
      End Sub
      

      ***** I've had it working a couple times on my workstation but never from the webserver. I usually get "Unknown username or password" or "Access to ..... denied!" I have identity impersonate="true" in the Web.Config.

      modified on Thursday, November 11, 2010 10:55 AM

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Please use proper formatting when posting code snippets. Use the "code block" menu item to wrap all code in

      tags


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups