Hallo,
ich möchte ein GridView mit Daten aus einer Access-DB verbinden. Diese liegt auf einem gesicherten Netzlaufwerk. Da der IIS unter einer anderen Freigabe als das Netzlaufwerk läuft benutze ich Impersonation. Diese funktioniert auch. D. h. ich bekomme Zugriff zum Laufwerk. An der Stelle
TempAdapter.Fill(ds) kommt jedoch der Fehler 'Unbekanter Fehler'
Wo könnte da das Problem liegen?
Hier ein Codeauszug:
<script type="text/VB" runat=server>
Public Class Impersonation
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Dim impersonationContext As WindowsImpersonationContext
Declare Function LogonUserA 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 DuplicateToken Lib "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As _
IntPtr) As Long
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 CBool(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
Sub undoImpersonation()
impersonationContext.Undo()
End Sub
End Class
Overloads Sub DB_öffnen(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
' DataSet erstellen
Dim mSQL As String = "SELECT * FROM [Geraete]"
Dim mI As New Impersonation
If mI.impersonateValidUser("user", "domain", "xxxx") Then
Dim ds As New Data.DataSet
Dim UpdatePath As String = "\\server\path\"
Dim _ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data" & _
"Source=\\server\path\Geraete.mdb;Persist Security" & _
"Info=False;Password=;User ID=;Jet OLEDB:Database" & _
"Password=xxxx;Jet OLEDB:Database Locking Mode=1"
Try
Dim TempAdapter = New OleDbDataAdapter(mSQL, _ConnectionString)
TempAdapter.Fill(ds)
' Das DataSet mit dem GridView-Objekt verbinden
GridView1.DataSource = ds
GridView1.DataBind()
StatusLabel.Text = _
System.Security.Principal.WindowsIdentity.GetCurrent.Name & _
IO.File.Exists(IO.Path.Combine(UpdatePath, "Geraete.mdb"))
Catch ex As Exception
StatusLabel.Text = ex.Message & _
System.Security.Principal.WindowsIdentity.GetCurrent.Name & _
IO.File.Exists(IO.Path.Combine(UpdatePath, "Geraete.mdb"))
Finally
mI.undoImpersonation()
End Try
Else
StatusLabel.Text = "Failed " & _
Threading.Thread.CurrentPrincipal.Identity.Name
End If
End If
End Sub
</script> Die Web-config endhält die nötigen Daten. Der IIS ist auf Windows-Authorisierung eingestellt.
<authentication mode="Windows"/>
<identity impersonate="true"/> Mit freundlichen Grüßen
Angelus19 |