如何给子线程做impersonate?

ElwinLuo 2009-04-18 02:00:23
Asp.net 程序中可以impersonate 一个帐号用来访问网络资源,但是如何给新建线程做impersonate ?
因为程序中调用了一个外部exe程序,
Process proc = new Process();
proc.StartInfo.FileName = "***.exe";
proc.start();
return proc.StandardOutput.ReadToEnd();

我试过用ShellExecute调用外部exe程序,
可是不能得到output...

...全文
180 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ElwinLuo 2009-04-23
  • 打赏
  • 举报
回复
谢谢Garnett_KG ,我后来使用CreateProcessWithLogonW 解决了这个问题,不过碰到的新问题是不能获取输出,谁能帮我看看, 谢谢。
请看下面这个链接:
http://topic.csdn.net/u/20090423/11/75a6f0ef-c77c-4197-b106-cde9da7d582a.html
ElwinLuo 2009-04-20
  • 打赏
  • 举报
回复
hi Garnett_KG,
我尝试用了你的代码去给一个进程impersonate后去访问网络资源,(部署在IIS上, 用ASP.net 调用)
结果还是显示无法获取网络资源...

该进程直接调用是可以的。
悔说话的哑巴 2009-04-20
  • 打赏
  • 举报
回复
这个不是很懂,不敢乱说帮顶
ElwinLuo 2009-04-20
  • 打赏
  • 举报
回复
windows identity of asp.net server:

windowsIdentity.Name: domain\UserName
windowsIdentity.IsAuthenticated: True
windowsIdentity.ImpersonationLevel: Impersonation
windowsIdentity.IsSystem: False



windows identity of child process:

windowsIdentity.Name: NT AUTHORITY\NETWORK SERVICE
windowsIdentity.IsAuthenticated: True
windowsIdentity.ImpersonationLevel: None
windowsIdentity.IsSystem: False

任何意见都表示感谢。
ElwinLuo 2009-04-20
  • 打赏
  • 举报
回复
在父进程中 WindowsIdentity.Name = "domain\userName",
在子进程中,就变成了 "NT AUTHORITY\NETWORK SERVICE"
ElwinLuo 2009-04-20
  • 打赏
  • 举报
回复
asp.net 中使用 <identity impersonate="true" userName="accountname" password="password" />做配置,
只针对于asp.net 当前的线程是没有问题的,
关键是子进程中的Windows identity name 却跟父进程的windows identity name 不同了。
Garnett_KG 2009-04-20
  • 打赏
  • 举报
回复
Garnett_KG 2009-04-20
  • 打赏
  • 举报
回复
asp.net是怎么配置的?
ElwinLuo 2009-04-20
  • 打赏
  • 举报
回复
Hi Garnett_KG:
用了你的代码后创建的new process的信息是

Name: NT AUTHORITY\NETWORK SERVICE

IsAuthenticated: True

ImpersonationLevel: None

IsSystem: False

跟我在asp.net 配置的impersonate unsername 不一样啊....
a2220046 2009-04-18
  • 打赏
  • 举报
回复
帮顶一下~~~
Garnett_KG 2009-04-18
  • 打赏
  • 举报
回复

internal static class NativeMethods
{
// WIN32 API
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentThread();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool ImpersonateSelf(int level);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool OpenThreadToken(IntPtr thread, uint desiredAccess, [MarshalAs(UnmanagedType.Bool)] bool openAsSelf, ref IntPtr handle);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool RevertToSelf();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool SetThreadToken(IntPtr thread, IntPtr token);
}
....

IntPtr currentToken=IntPtr.Zero;
IntPtr currentThread = NativeMethods.GetCurrentThread();
NativeMethods.OpenThreadToken(currentThread, 4, true, ref currentToken);
NativeMethods.SetThreadToken(IntPtr.Zero, IntPtr.Zero);

NativeMethods.ImpersonateSelf(2);
Process proc = new Process();
proc.StartInfo.FileName = "***.exe";
proc.start();
string output=proc.StandardOutput.ReadToEnd();


NativeMethods.RevertToSelf();
NativeMethods.SetThreadToken(IntPtr.Zero, currentToken);
return output;

ElwinLuo 2009-04-18
  • 打赏
  • 举报
回复
我尝试给子线程加username 和 password;
proc.StartInfo.Domain = "domain";
proc.StartInfo.UserName = "username";
string password = "password";

SecureString secureString = new SecureString();
foreach (char c in password.ToCharArray())
{
secureString.AppendChar(c);
}
proc.StartInfo.Password = secureString;
部署到IIS上,运行时出现的错误是: The application failed to initialize properly (0xc0000142). Click on OK to terminate the application.

any comments or suggestions are appreciated.
ElwinLuo 2009-04-18
  • 打赏
  • 举报
回复
好像不行啊,调用ImpersonateSelf之后显示的错误是:
[HttpException (0x80004005): An error occurred while attempting to impersonate. Execution of this request cannot continue.]
System.Web.ImpersonationContext.GetCurrentToken() +8845961
System.Web.ImpersonationContext.get_CurrentThreadTokenExists() +58
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +193
System.Web.ApplicationStepManager.ResumeSteps(Exception error) +501
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +123
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +379
ZJ159 2009-04-18
  • 打赏
  • 举报
回复
yagebu1983 2009-04-18
  • 打赏
  • 举报
回复
委托。。。
Garnett_KG 2009-04-18
  • 打赏
  • 举报
回复

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError=true)]
internal static extern bool ImpersonateSelf(int level);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError=true)]
internal static extern bool RevertToSelf();


...
ImpersonateSelf(2)

Process proc = new Process();
proc.StartInfo.FileName = "***.exe";
proc.start();
string output=proc.StandardOutput.ReadToEnd();

RevertToSelf()

return output;







kfps8kfps8 2009-04-18
  • 打赏
  • 举报
回复
帮顶

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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