怎样用C#实现runas功能?

jinn 2004-11-29 04:26:11
在我的C#的代码里要起动另一个程序,但必需要用另一个用户身份(比如:Administrator)来执行.
也就是在执行另个程序时带上另一个用户的用户名和密码。
我用Process.Start("可执行程序名");但不能带上用户名和密码。
请问有什么方法可以实现?
...全文
425 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinn 2004-12-01
  • 打赏
  • 举报
回复
参考tiaoci的思想后,我用API函数CreateProcessWithLogonW 实现我的目标。现付上代码供参考:

StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public Int32 dwProcessID;
public Int32 dwThreadID;
}

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool CreateProcessWithLogonW(String lpszUsername,
String lpszDomain, String lpszPassword,
int dwLogonFlags, string applicationName, string commandLine,
int creationFlags, IntPtr environment,
string currentDirectory,
ref STARTUPINFO sui,
out PROCESS_INFORMATION processInfo);

[DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource,
int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr *Arguments);

const int LOGON_WITH_PROFILE = 1;
const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
const int NORMAL_PRIORITY_CLASS = 0x00000020;

private void BtBrowse_Click(object sender, System.EventArgs e)
{
string UserName, DomainName, Password;
DomainName = Domain.Text;
UserName = Usn.Text;
Password = Pwd.Text;
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartInfo = new STARTUPINFO();
StartInfo.cb = Marshal.SizeOf(StartInfo);
StartInfo.lpTitle = "Mike Process Creator";
StartInfo.dwFlags = STARTF_USECOUNTCHARS;
StartInfo.dwYCountChars = 50;
bool results = CreateProcessWithLogonW(UserName,DomainName,Password,LOGON_WITH_PROFILE,
null,"notepad.exe d:\\log.txt",NORMAL_PRIORITY_CLASS
|CREATE_UNICODE_ENVIRONMENT,IntPtr.Zero,null,ref StartInfo,out ProcessInfo);
if (false == results)
{
int ret = Marshal.GetLastWin32Error();
string msg;
msg = "LogonUser failed with error code : " + ret.ToString();
MessageBox.Show(msg);
MessageBox.Show("\nError: " + ret.ToString() + "\n" +GetErrorMessage(ret).ToString());
return;
}

public unsafe static string GetErrorMessage(int errorCode)
{
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
int messageSize = 255;
String lpMsgBuf = "";
int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
IntPtr ptrlpSource = IntPtr.Zero;
IntPtr prtArguments = IntPtr.Zero;
int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &prtArguments);
if (0 == retVal)
{
throw new Exception("Failed to format message for error code " + errorCode + ". ");
}
return lpMsgBuf;
}

}
jinn 2004-11-30
  • 打赏
  • 举报
回复
to tiaoci:
能详细点吗?急!解决后散分!
tiaoci 2004-11-29
  • 打赏
  • 举报
回复
Api

LogonUser, 然后 ImpersonateLoggedOnUser,

然后创建进程CreateProcessAsUser
北京的雾霾天 2004-11-29
  • 打赏
  • 举报
回复
想办法中.......
jinn 2004-11-29
  • 打赏
  • 举报
回复
我要的是C#代码实现,不是用Runas来实现!
请大侠指教!分不只100...
北京的雾霾天 2004-11-29
  • 打赏
  • 举报
回复
http://search.csdn.net/Expert/topic/1273/1273886.xml?temp=.2720301
北京的雾霾天 2004-11-29
  • 打赏
  • 举报
回复
参考下:
http://search.csdn.net/Expert/topic/646/646255.xml?temp=.2810175
北京的雾霾天 2004-11-29
  • 打赏
  • 举报
回复
难哟,帮顶,看一下高人怎么来说.

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧