C#webbrowser控制和模拟 [英] C# webbrowser control and impersonation

查看:177
本文介绍了C#webbrowser控制和模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



我的问题背景是我必须为部门中的其他人提供的监控解决方案(SCOM)。



我希望我能够看到每个用户组的其他人所能看到的相同内容。由于我正在与其他人一起工作,我总是有额外的工作,我可以看到他们在有限的权利中可以看到什么。



我现在想创建一个小应用程序(winform),它允许我在一个应用程序中观察所有不同组的网页观点。所有结果集都在同一个网页上,但对于用户而言则不同。



我喜欢做的是:

- 创建一个带有每个用户组标签的应用程序。

- 为每个用户组配置一个虚拟帐户,该帐户与该组用户具有相同的权限(实际上是同一个Windows组的成员)

- 现在创建一个webbrowser控件并使用虚拟账户的凭证



我想到的解决方案:

- 创建一个usercontrol,它将接受构造函数的URL, usernamen和密码

- 在运行时读取配置并为每个项目创建一个带有usercontrol的选项卡



问题:

如何在不同的安全上下文中创建usercontrol?

我试图使用UriBuilder的用户名和密码,但结果根本不起作用。



有没有人有想法?



最好的问候

Dirk

解决方案

组合来自2个链接的代码:

使用C#在Windows中访问受密码保护的网络驱动器?

在C#.NET中进行完整的模拟演示

并执行以下步骤

  //   1。登录 
IntPtr 令牌;
IntPtr tokenD;
if (!NativeMethods。 LogonUser (login.User,login.Domain,login.Password,LogonType.Interactive,LogonProvider。默认, out 令牌))
throw new Win32Exception();
if (!NativeMethods。 DuplicateToken (token,SecurityImpersonationLevel.Impersonation, out tokenD))
throw new Win32Exception();

// 2。剖析。您需要管理员或本地帐户才能运行LoadUserProfile
ProfileInfo profileInfo = new ProfileInfo();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.lpUserName = login.User;
profileInfo.dwFlags = 1 ;
if (!NativeMethods。 LoadUserProfile (tokenD, ref profileInfo ))
throw new Win32Exception();

// 3。模拟
var identity = new WindowsIdentity(tokenD);
var context = identity.Impersonate();

// 5。创建浏览器
var browser = new WebBrowser ();
browser.Tag = context;
browser.Navigate( new Uri( http://www.codeproject.com));

// 6。在用户之后处理,可以在WebBrowser.Disposing或在选项卡上进行尝试选择事件
NativeMethods。 CloseHandle (tokenD);
NativeMethods。 CloseHandle (令牌);
NativeMethods。 UnloadUserProfile (tokenD,profile.hProfile);
context.Undo();



这个老问题。也许你有另一种解决方案?如果是,请发布。


Hi friends

Background of my question is a monitoring solution (SCOM) that I have to provide for others in the department.

I like to have the possibility that I can see the same what the other employes of each usergroup can see. As I'm working with more rights as the others I always have extra work that I can see what they with the limited rights can see.

I'm like to create now a small application (winform) that allows me to observe the webpage for all of the different groups in a single application with all the different point of views. All the resultsets are on the same webpage but are different for the users.

What I like to do is:
- creating an application with a tab for each usergroup.
- configure for each usergroup a "dummy" account that has the same rights as the users of the group (in fact is a member of the same windows group)
- now create a webbrowser control and using the credentials of the dummy account

Solution that I have in mind:
- creating an usercontrol that will take in the constructor the URL, the usernamen and the password
- on runtime read the configurations and create for each of the items a tab with the usercontrol

Problem:
How can I create the usercontrol in a different security context?
I was trying to use the UriBuilder with username and password but the result was not working at all.

Has anyone an idea?

best regards
Dirk

解决方案

Combine code from 2 links:
Accessing Password Protected Network Drives in Windows in C#?
A complete Impersonation Demo in C#.NET
And do folowing steps

//1. Logining
IntPtr token;
IntPtr tokenD;
if (!NativeMethods.LogonUser(login.User, login.Domain, login.Password, LogonType.Interactive, LogonProvider.Default, out token))
    throw new Win32Exception();
if (!NativeMethods.DuplicateToken(token, SecurityImpersonationLevel.Impersonation, out tokenD))
    throw new Win32Exception();

//2. Profiling. You nead administranor or local account to run LoadUserProfile
ProfileInfo profileInfo = new ProfileInfo();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.lpUserName = login.User;
profileInfo.dwFlags = 1;
if(!NativeMethods.LoadUserProfile(tokenD, ref profileInfo))
    throw new Win32Exception();

//3. Impersonating
var identity = new WindowsIdentity(tokenD);
var context = identity.Impersonate();

//5. Create Browser
var browser = new WebBrowser();
browser.Tag = context;
browser.Navigate(new Uri("http://www.codeproject.com"));

//6. Dispose after user, can bee on WebBrowser.Disposing or try it on tabs Selection event
NativeMethods.CloseHandle(tokenD);
NativeMethods.CloseHandle(token);
NativeMethods.UnloadUserProfile(tokenD, profile.hProfile);
context.Undo();


Its old question. Maybe you have another solution? If so, please post it.


这篇关于C#webbrowser控制和模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆