سلام
من مدتها توی این سایت و سایتهای دیگه دنبال روشی میگشتم که با آن بتونم به یک پوشه در شبکه با یوزری متفاوت با اونکه به ویندوز وارد شدیم متصل بشم و سوالها پرسید و به نتیجه نرسیدم تا بالاخره بعد از مدتها اون توی یک سایت لاتین یافتم.(اینم رعایت حق کپی رایت)
البته زیاد از اینکه چه جوری عمل میکنه سر در نمیارم ولی بدرستی کار میکنه.
حالا اینجا میزارم که دوستانی که مثل من دنبالش بودن ازش برای بالابردن امنیت اطلاعات در برنامشون استفاده کنن:
اول باید یک کلاس جدید بسازیم و این کدها رو داخل بریزیم (البته خط 7 و 8 رو بدلیل اینکه وارنینگ میداد حذف کردم و خللی وارد نشد) :
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Imports Microsoft.VisualBasic
<Assembly: SecurityPermissionAttribute(SecurityAction.Request Minimum, UnmanagedCode:=True), _
Assembly: PermissionSetAttribute(SecurityAction.RequestMinim um, Name:="FullTrust")>
Public Class RunAs_Impersonator
#Region "Private Variables and Enum Constants"
Private tokenHandle As New IntPtr(0)
Private dupeTokenHandle As New IntPtr(0)
Private impersonatedUser As WindowsImpersonationContext
#End Region
#Region "Properties"
#End Region
#Region "Public Methods"
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
Public Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Boolean
' Test harness.
' If you incorporate this code into a DLL, be sure to demand FullTrust.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Sub ImpersonateStart(ByVal Domain As String, ByVal userName As String, ByVal Password As String)
Try
tokenHandle = IntPtr.Zero
' Call LogonUser to obtain a handle to an access token.
Dim returnValue As Boolean = LogonUser(userName, Domain, Password, 2, 0, tokenHandle)
'check if logon successful
If returnValue = False Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("LogonUser failed with error code : {0}", ret)
Throw New System.ComponentModel.Win32Exception(ret)
Exit Sub
End If
'Logon succeeded
' Use the token handle returned by LogonUser.
Dim newId As New WindowsIdentity(tokenHandle)
impersonatedUser = newId.Impersonate()
Catch ex As Exception
Throw ex
Exit Sub
End Try
End Sub
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Sub ImpersonateStop()
' Stop impersonating the user.
impersonatedUser.Undo()
' Free the tokens.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
End Sub
#End Region
#Region "Private Methods"
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
<DllImport("kernel32.dll")> _
Public 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
#End Region
End Class
و هرجای برنامه که خواستیم برناممون با یک یوزر جدید به شبکه وصل بشه از کد زیر استفاده میکنیم:
Dim imp As New RunAs_Impersonator '(somewhere in your declarations)
imp.ImpersonateStart("YourDomain", "UserName", "UserPassword")
و هرجا خواستم دوباره به حالت اول برگرده از این کد استفاده می کنیم :
imp.ImpersonateStop()
حالا میتونید یه پوشه تو سرورتون بسازید (مثلا پوشه ای که عکسها و یا مدارک پرسنلی رو که لازم دارید تو برنامتون نمایش بده رو توش بریزید) و بعد اونرو شیر کنید و توی پرمیشنش فقط یوزری رو که برناممون قراره وارد کنه اد کنید و بقیه رو پاک کنید. به این ترتیب کاربر وقتی وارد ویندوز میشه نمیتونه به محتویات پوشتون دسترسی پیدا کنه و فقط و فقط برناتون میتونه دسترسی داشته باشه.
راستی تا یادم نرفته باید یک یوزر و پسورد مشابه هم توی کامپیوترتون (کلاینت) و هم توی سرور ایجاد کنید و توی برنامتون ازش استفاده کنید.
همین نکته مدتها منو سرکار گذاشته بود و من فقط توی سرور میساختم یوزر نیم و پسورد رو و توی کلاینت نمیساختم و سیستم عمل نمیکرد و اتفاقی به این روش (خودساخته) رسیدم. البته منتطقا باید همون توی سرور کافی باشه ولی نیست اگه کسی از اساتید میدونه بفرماید تا ماهم بیاموزیم.