Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Security / February 2008

Tip: Looking for answers? Try searching our database.

System.Security.Principal.WindowsIdentity file.copy

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vinod - 08 Feb 2008 16:26 GMT
I have follwing code it works only from the same system but not from the
other client or system.

 strusername = Me.txtUserId.Text
       strpassword = Me.txtpassword.Text
       strDomain = ddlDomain.SelectedItem.Text

Session("Username")=strusername
       Session("Password") = strpassword
       Session("Domain") = strDomain

'to get username password domain when ever user logs in and store username
password and domain to session

Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal
Username As String, ByVal Password As String) As Boolean

       Dim Success As Boolean = False
       Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" &
Domain, Username, Password)
       Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
       Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
       Try
           Dim Results As System.DirectoryServices.SearchResult =
Searcher.FindOne
           Success = Not (Results Is Nothing)
       Catch
           Success = False
       End Try
       Return Success
   End Function

'on button click
Dim UserName, Password, Domain As String

                   UserName = Session("Username")
                   Password = Session("Password")
                   Domain = Session("Domain")
                   Dim aa As New AliasAccount(Domain & "\" & UserName,
Password)
                   aa.BeginImpersonation()

                   Dim thePath As String
                   Dim destPath As String

                   File.Copy(sPath, sFullPath, 1)

                   aa.EndImpersonation()

Public Class AliasAccount
   Private _username, _password, _domainname As String

       Private _tokenHandle As New IntPtr(0)
       Private _dupeTokenHandle As New IntPtr(0)
       Private _impersonatedUser As
System.Security.Principal.WindowsImpersonationContext

       Public Sub New(ByVal username As String, ByVal password As String)
           Dim nameparts() As String = username.Split("\")
           If nameparts.Length > 1 Then
               _domainname = nameparts(0)
               _username = nameparts(1)
           Else
               _username = username
           End If
           _password = password
       End Sub

       Public Sub New(ByVal username As String, ByVal password As String,
ByVal domainname As String)
           _username = username
           _password = password
           _domainname = domainname
       End Sub

       Public Sub BeginImpersonation()
           Const LOGON32_PROVIDER_DEFAULT As Integer = 0
           Const LOGON32_LOGON_INTERACTIVE As Integer = 2
           Const SecurityImpersonation As Integer = 2

           Dim win32ErrorNumber As Integer

           _tokenHandle = IntPtr.Zero
           _dupeTokenHandle = IntPtr.Zero

           If Not LogonUser(_username, _domainname, _password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
               win32ErrorNumber =
System.Runtime.InteropServices.Marshal.GetLastWin32Error()
               Throw New ImpersonationException(win32ErrorNumber,
GetErrorMessage(win32ErrorNumber), _username, _domainname)
           End If

           If Not DuplicateToken(_tokenHandle, SecurityImpersonation,
_dupeTokenHandle) Then
               win32ErrorNumber =
System.Runtime.InteropServices.Marshal.GetLastWin32Error()

               CloseHandle(_tokenHandle)
               Throw New ImpersonationException(win32ErrorNumber, "Unable
to duplicate token!", _username, _domainname)
           End If

           Dim newId As New
System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
           _impersonatedUser = newId.Impersonate()
       End Sub

       Public Sub EndImpersonation()
           If Not _impersonatedUser Is Nothing Then
               _impersonatedUser.Undo()
               _impersonatedUser = Nothing

               If Not System.IntPtr.op_Equality(_tokenHandle, IntPtr.Zero)
Then
                   CloseHandle(_tokenHandle)
               End If
               If Not System.IntPtr.op_Equality(_dupeTokenHandle,
IntPtr.Zero) Then
                   CloseHandle(_dupeTokenHandle)
               End If
           End If
       End Sub

       Public ReadOnly Property username() As String
           Get
               Return _username
           End Get
       End Property

       Public ReadOnly Property domainname() As String
           Get
               Return _domainname
           End Get
       End Property

       Public ReadOnly Property currentWindowsUsername() As String
           Get
               Return
System.Security.Principal.WindowsIdentity.GetCurrent().Name
           End Get
       End Property

#Region "Exception Class"
       Public Class ImpersonationException
           Inherits System.Exception

           Public ReadOnly win32ErrorNumber As Integer

           Public Sub New(ByVal win32ErrorNumber As Integer, ByVal msg As
String, ByVal username As String, ByVal domainname As String)
               MyBase.New(String.Format("Impersonation of {1}\{0} failed!
[{2}] {3}", username, domainname, win32ErrorNumber, msg))
               Me.win32ErrorNumber = win32ErrorNumber
           End Sub
       End Class
#End Region

#Region "External Declarations and Helpers"
       Private 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 Boolean

       Private Declare Auto Function DuplicateToken Lib "advapi32.dll"
(ByVal ExistingTokenHandle As IntPtr, _
                   ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
                   ByRef DuplicateTokenHandle As IntPtr) As Boolean

       Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
handle As IntPtr) As Boolean

       <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
       Private Shared Function FormatMessage(ByVal dwFlags As Integer,
ByRef lpSource As IntPtr, _
               ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer,
ByRef lpBuffer As [String], _
               ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
       End Function

       Private Function GetErrorMessage(ByVal errorCode As Integer) As String
           Dim FORMAT_MESSAGE_ALLOCATE_BUFFER As Integer = &H100
           Dim FORMAT_MESSAGE_IGNORE_INSERTS As Integer = &H200
           Dim FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000

           Dim messageSize As Integer = 255
           Dim lpMsgBuf As String
           Dim dwFlags As Integer = FORMAT_MESSAGE_ALLOCATE_BUFFER Or
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS

           Dim ptrlpSource As IntPtr = IntPtr.Zero
           Dim prtArguments As IntPtr = IntPtr.Zero

           Dim retVal As Integer = FormatMessage(dwFlags, ptrlpSource,
errorCode, 0, lpMsgBuf, messageSize, prtArguments)
           If 0 = retVal Then
               Throw New System.Exception("Failed to format message for
error code " + errorCode.ToString() + ". ")
           End If

           Return lpMsgBuf
       End Function

#End Region

   
End Class
Joe Kaplan - 09 Feb 2008 00:41 GMT
How does it fail?  What are the environment differences?  How do you expect
to get any help without providing more details?

Joe K.

Signature

Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--

>I have follwing code it works only from the same system but not from the
> other client or system.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.