Ask a Question related to ASP.NET Security, Design and Development.
-
Maurice Thompson via .NET 247 #1
Impersonation problem - logs on ok but no file access ?? Please help!
Hi,
I am writing a windows forms app and i need to access a fileshare but impersonating another user (rather than the userlogged on). All my code seems to work, i.e the impersonated usergets logged on etc.. However when i try to access a file on thenetwork i doesnt work. It doesnt work even when i use theadministrators logon details ??
Any help appreciated as im at a dead end!!
Thanks in advance..
<code>
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.IO
<Assembly:SecurityPermissionAttribute(SecurityActi on.RequestMinimum,UnmanagedCode:=True)>
Public Class clsSecurity
Private idWindows As WindowsIdentity
Private prinWindows As WindowsPrincipal
<DllImport("C:\Windows\System32\advapi32.dll")> _
Public Shared Function LogonUser(ByVal lpszUsername AsString, _
ByVal lpszDomain AsString, _
ByVal lpszPassword AsString, _
ByVal dwLogonType AsInteger, _
ByVal dwLogonProvider AsInteger, _
ByRef phToken As Integer)As Boolean
End Function
<DllImport("C:\Windows\System32\Kernel32.dll")> _
Public Shared Function GetLastError() As Integer
End Function
'Public Shared Sub Main(ByVal args() As String)
Public Sub testme()
'The Windows NT user token.
Dim token1 As Integer
'The parameters for LogonUser are the user name, computername, password,
'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logonprovider (LOGON32_PROVIDER_DEFAULT),
'and user token.
Dim loggedOn As Boolean = LogonUser("test", "MYDOMAIN","test", 3, 0, token1)
Console.WriteLine("LogonUser called")
''''Call GetLastError to try to determine why logonfailed if it did not succeed.
'''Dim ret As Integer = GetLastError()
Console.WriteLine("LogonUser Success? " &loggedOn.ToString)
Console.WriteLine("NT Token Value: " & token1.ToString)
'Starting impersonation here:
Console.WriteLine("Before impersonation:")
Dim mWI1 As WindowsIdentity =WindowsIdentity.GetCurrent()
Console.WriteLine(mWI1.Name)
Console.WriteLine(mWI1.Token)
'see if access to network file ispossible................
Console.WriteLine("file exists = " &File.Exists("K:\test.txt"))
Dim token2 As IntPtr = New IntPtr(token1)
Console.WriteLine("New identity created:")
Dim mWI2 As WindowsIdentity = NewWindowsIdentity(token2)
Console.WriteLine(mWI2.Name)
Console.WriteLine(mWI2.Token)
'Impersonate the user.
Dim mWIC As WindowsImpersonationContext =mWI2.Impersonate()
Console.WriteLine("After impersonation:")
Dim mWI3 As WindowsIdentity =WindowsIdentity.GetCurrent()
Console.WriteLine(mWI3.Name)
Console.WriteLine(mWI3.Token)
'see if access to network file ispossible................
Console.WriteLine("file exists = " &File.Exists("K:\test.txt"))
'Revert to previous identity.
mWIC.Undo()
Console.WriteLine("After impersonation is reverted:")
Dim mWI4 As WindowsIdentity =WindowsIdentity.GetCurrent()
Console.WriteLine(mWI4.Name)
Console.WriteLine(mWI4.Token)
'see if access to network file ispossible................
Console.WriteLine("file exists = " &File.Exists("K:\test.txt"))
End Sub
End Class
</code>
--------------------------------
From: Maurice Thompson
-----------------------
Posted by a user from .NET 247 ([url]http://www.dotnet247.com/[/url])
<Id>rOwWNjpum0aXVb4fWQH9JA==</Id>
Maurice Thompson via .NET 247 Guest
-
FMS Access Logs
Is there a way to save additional information in the FMS access logs when a stream is started? I was hoping to save a "user id" in the log, so I... -
ASP.NET Impersonation to access Oracle database...
Hi all, I am trying to work out how I can use impersonation to connect to an Oracle 9i database such that I can avoid sending a clear text... -
Access Denied using Impersonation
I am developing an intranet application that uses windows authentication and impersonation. The problem I'm having is that it seems when the... -
impersonation and ado access connection
I am implementing impersonation in my machine.config for IIS application Isolation of the ASPNET worker process. I am giving the new account the... -
Access Denied and Impersonation
Hi, I need to use impersonation in in my ASP.NET applicaiton. But when I set it to "true" in the Web.config file, the "Access Denied" message...



Reply With Quote

