- ظهر وكود لانتحال شخصية المستخدمين...
حسناً، كان لديّ استراحة جميلة... الآن لبعض الرموز، هذا سيجعلك "تُصدّر" مستخدماً - مفيد جداً عندما تحاول، على سبيل المثال، تحميل ملف إلى موقع شبكة... كالمعتاد، لا فكرة أين وجدت هذا، إذا كان أنت، أعلمني!
using System;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace Components.Security
{
///
/// Summary description for ImpersonateUser.
///
public class ImpersonateUser
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LogonUser( string lpszUserUname,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
public bool impersonateValidUser(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
return (impersonationContext != null);
}
else
{
return false;
}
}
else
{
return false;
}
}
public void undoImpersonation()
{
impersonationContext.Undo();
}
}
}
Finding related posts...