可将 GenericIdentity 类和 GenericPrincipal 类合起来使用,以创建独立于 Windows NT 或 Windows 2000 域的身份验证方案。例如,使用这两个对象的应用程序可能会提示用户输入姓名和密码,并将其同数据库项进行核对,然后根据数据库中的值创建标识和用户对象。参考 ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconcreatinggenericprincipalgenericidentityobjects.htm
FileInfo fi = new FileInfo( 源文件 ); string name = @"\\服务器文件名\tt.txt"; fi.CopyTo( name );这样有什么问题吗?
WindowsIdentity MyIdentity = new WindowsIdentity(hToken); -- 其中 hToken 表示一个 Windows 标记。此标记通常通过调用非托管代码(如调用 Win32 API LogonUser)来检索 WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);System.Threading.Thread.CurrentPrincipal = MyPrincipal;
我记得有一段获得hToken的代码 忘记在那里的 找到了贴给你 :)
这是一段获取htoken的程序using System; using System.Runtime.InteropServices; using System.Security.Principal; using System.Security.Permissions;[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode=true)] public class Class1 { [DllImport("C:\\WINNT\\System32\\advapi32.dll")] public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken); [DllImport("C:\\WINNT\\System32\\Kernel32.dll")] public static extern int GetLastError(); public static void Main(string[] args) { // The Windows NT user token. int token1; // Get the user token for the specified user, machine, and password using the unmanaged LogonUser method. bool loggedOn = LogonUser( // User name. "", // Computer name. "", // Password. "", // Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT. 3, // Logon provider = LOGON32_PROVIDER_DEFAULT. 0, // The user token for the specified user is returned here. out token1);
Console.WriteLine("LogonUser called");
// Call GetLastError to try to determine why logon failed if it did not succeed. int ret = GetLastError();
Console.WriteLine("LogonUser Success? " + loggedOn); Console.WriteLine("NT Token Value: " + token1); if (ret != 0) Console.WriteLine("Error code (126 == \"Specified module could not be found\"): " + ret);
顺便说一下 你必须知道你要获取的有效用户 user name 和Passwork 不然是没办法的(那就不是写程序,到可以去做黑客了) 祝成功~~~
你试试这一段,我没有环境没测试int token1; // Get the user token for the specified user, machine, and password using the unmanaged LogonUser method. string uname = 用户; string upwd = 密码; string uDomain = 域名; bool loggedOn = LogonUser( // User name. uname, // Domain or Computer name. uDomain, // Password. upwd, // Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT. 3, // Logon provider = LOGON32_PROVIDER_DEFAULT. 0, // The user token for the specified user is returned here. out token1); //string s = WindowsIdentity.GetCurrent().Token.ToString(); //Thread.CurrentPrincipal.Identity
System.IntPtr token2; //token2 = WindowsIdentity.GetCurrent().Token; token2 = new IntPtr( token1 ); WindowsIdentity MyIdentity = new WindowsIdentity( token2 ); WindowsPrincipal MyPrincipal = new WindowsPrincipal( MyIdentity ); Thread.CurrentPrincipal = MyPrincipal; FileInfo fi = new FileInfo( 源文件名 ); //string name = textBox2.Text + "tt.txt"; string name = @"\\共享目录\tt.txt"; fi.CopyTo( name );
ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconcreatinggenericprincipalgenericidentityobjects.htm
string name = @"\\服务器文件名\tt.txt";
fi.CopyTo( name );这样有什么问题吗?
获取或设置线程的当前负责人(对基于角色的安全性而言)。如果需要权限
创建设定你需要的用户身份
然后付给当前线程
当然有问题,你不登陆,怎么把文件Copy过去,没有权限啊
-- 其中 hToken 表示一个 Windows 标记。此标记通常通过调用非托管代码(如调用 Win32 API LogonUser)来检索
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);System.Threading.Thread.CurrentPrincipal = MyPrincipal;
忘记在那里的
找到了贴给你
:)
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode=true)]
public class Class1
{
[DllImport("C:\\WINNT\\System32\\advapi32.dll")]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken); [DllImport("C:\\WINNT\\System32\\Kernel32.dll")]
public static extern int GetLastError(); public static void Main(string[] args)
{
// The Windows NT user token.
int token1; // Get the user token for the specified user, machine, and password using the unmanaged LogonUser method. bool loggedOn = LogonUser(
// User name.
"", // Computer name.
"", // Password.
"", // Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT.
3, // Logon provider = LOGON32_PROVIDER_DEFAULT.
0, // The user token for the specified user is returned here.
out token1);
Console.WriteLine("LogonUser called");
// Call GetLastError to try to determine why logon failed if it did not succeed.
int ret = GetLastError();
Console.WriteLine("LogonUser Success? " + loggedOn);
Console.WriteLine("NT Token Value: " + token1);
if (ret != 0) Console.WriteLine("Error code (126 == \"Specified module could not be found\"): " + ret);
//Starting impersonation here:
Console.WriteLine("\n\nBefore impersonation:\n");
WindowsIdentity mWI1 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI1.Name);
Console.WriteLine(mWI1.Token); IntPtr token2 = new IntPtr(token1); Console.WriteLine("\n\nNew identity created:\n");
WindowsIdentity mWI2 = new WindowsIdentity(token2);
Console.WriteLine(mWI2.Name);
Console.WriteLine(mWI2.Token); // Impersonate the user.
WindowsImpersonationContext mWIC = mWI2.Impersonate(); Console.WriteLine("\n\nAfter impersonation:\n");
WindowsIdentity mWI3 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI3.Name);
Console.WriteLine(mWI3.Token); // Revert to previous identity.
mWIC.Undo(); Console.WriteLine("\n\nAfter impersonation is reverted:\n");
WindowsIdentity mWI4 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI4.Name);
Console.WriteLine(mWI4.Token);
}
}
你必须知道你要获取的有效用户
user name 和Passwork
不然是没办法的(那就不是写程序,到可以去做黑客了)
祝成功~~~
string upwd = 密码;
string uDomain = 域名;
bool loggedOn = LogonUser(
// User name.
uname, // Domain or Computer name.
uDomain, // Password.
upwd, // Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT.
3, // Logon provider = LOGON32_PROVIDER_DEFAULT.
0, // The user token for the specified user is returned here.
out token1); //string s = WindowsIdentity.GetCurrent().Token.ToString();
//Thread.CurrentPrincipal.Identity
System.IntPtr token2;
//token2 = WindowsIdentity.GetCurrent().Token;
token2 = new IntPtr( token1 );
WindowsIdentity MyIdentity = new WindowsIdentity( token2 );
WindowsPrincipal MyPrincipal = new WindowsPrincipal( MyIdentity );
Thread.CurrentPrincipal = MyPrincipal; FileInfo fi = new FileInfo( 源文件名 );
//string name = textBox2.Text + "tt.txt";
string name = @"\\共享目录\tt.txt";
fi.CopyTo( name );
另,那个DoMain能不能是IP