Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Security.Principal;
Using System.Security.AccessControl;
Using System.Runtime.InteropServices;
Using System.Security.Permissions;
Namespace Changeuser
{
Class Program
{
[DllImport ("advapi32.dll", SetLastError = True)]
private static extern bool LogonUser (String lpszUserName, String lpszdomain, String lpszpassword, int dwlogontype, int DwL Ogonprovider, ref IntPtr phtoken);
[DllImport ("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle (INTPTR handle);
Get token after Login
private static IntPtr Tokenhandle = new IntPtr (0);
private static WindowsImpersonationContext Impersonateduser;
[PermissionSetAttribute (SecurityAction.Demand, Name = "FullTrust")]
private static bool Impersonate (string domainName, String userName, string password)
{
Try
{
const int logon32_provider_default = 0;
const int logon32_logon_interactive = 2;
Tokenhandle = IntPtr.Zero;
BOOL returnvalue = LogonUser (userName, domainName, password, logon32_logon_interactive, Logon32_provider_default, ref Tokenhandle);
if (!returnvalue)
{
int ret = Marshal.GetLastWin32Error ();
Console.WriteLine ("LogonUser call failed with error code:" + ret);
throw new System.ComponentModel.Win32Exception (ret);
}
WindowsIdentity newId = new WindowsIdentity (tokenhandle);
Impersonateduser = Newid.impersonate ();
return true;
}
catch (Exception ex)
{
Console.WriteLine ("Exception occurred." + ex. Message);
return false;
}
}
<summary>
Implements user context switching.
</summary>
private static void Undo ()
{
Try
{
WindowsIdentity currentidentity = WindowsIdentity.GetCurrent ();
Console.WriteLine ("First:" +currentidentity.user.tostring ());
Enable User Switching
Impersonateduser.undo ();
Currentidentity = WindowsIdentity.GetCurrent ();
Console.WriteLine ("Second:" + currentIdentity.User.ToString ());
Free the Tokens.
if (tokenhandle! = IntPtr.Zero)
CloseHandle (Tokenhandle);
}
catch (System.Exception ex)
{
Console.WriteLine ("Undo except:" + ex.) Message);
}
}
public static void Addfilesecurity (string fileName, String account,
Filesystemrights rights, AccessControlType ControlType)
{
Get a FileSecurity object that represents the
Current security settings.
FileSecurity fsecurity = File.getaccesscontrol (fileName);
Add the FileSystemAccessRule to the security settings.
Fsecurity.addaccessrule (New FileSystemAccessRule (account,
Rights, ControlType));
Set the new access settings.
File.setaccesscontrol (FileName, fsecurity);
}
static void Main (string[] args)
{
Try
{
if (args. Length < 3)
{
Console.WriteLine ("Info:");
Console.WriteLine ("EXE username password file:use username password zai path xia create index111.asp");
Return
}
String strusername = Args[0];
String strpass = Args[1];
String strfile = Args[2];
String strdomain = ".";
if (strusername.indexof ("\ \")! =-1)
{
Strdomain = strusername.split (' \ \ ') [0];
strUserName = strusername.split (' \ \ ') [1];
}
Impersonate (Strdomain, strUserName, Strpass);
if (file.exists (strfile))
{
Join access Control
Addfilesecurity (strfile, strUserName, Filesystemrights.fullcontrol, Accesscontroltype.allow);
Setting file properties
System.IO.File.SetAttributes (strfile, System.IO.FileAttributes.Normal);
File.delete (strfile);
Console.WriteLine ("Delete is ok\n");
}
Undo ();
}
catch (System.Exception ex)
{
Console.WriteLine ("Main except:" + ex.) Message);
}
Http://www.gutefreunde.ch/content/files/documents/en_admin.aspx
}
}
}
Windows implements User Switching