如何强制基于 IE 的 Web 浏览器控件使用特定的身份验证凭据?

发布于 2024-09-18 06:16:33 字数 357 浏览 8 评论 0原文

我有一个带有嵌入式 Web 浏览器控件的 WPF C# 客户端应用程序。我已准备好所有适当的挂钩,以便如果我正在浏览的网站需要身份验证,我会处理 IAuthenticate 并传入所需的凭据(用户已经登录到客户端应用程序本身)。这很好用,除了...

如果用户“Bob”通过 IE 访问该网站并输入他的用户名和密码,那么有人使用客户端应用程序并以“Steve”身份登录,“Bob”的会话仍然经过身份验证,并且该网站从不要求新的凭据,因此客户端以“Bob”身份连接。

我真正想做的是每次嵌入式浏览器连接到此站点时,我想发送凭据并强制浏览器和站点使用这些凭据。

有什么想法吗?

请注意,当我需要模拟不同的用户时,这在测试过程中更成为一个问题。

I have a WPF C# client app with an embedded webbrowser control. I have all of the proper hooks in place so that if the site I'm browsing to requires authentication, I handle the IAuthenticate and pass in the required credentials (the user has already logged in to the client app itself). That works great, except...

If user "Bob" visits the site through IE and enters his username and password, then someone uses the client app and logs in as "Steve", "Bob"s session is still authenticated and the site never asks for new credentials, so the client connects as "Bob".

What I really want to do is every time the embedded browser connects to this site, I want to send the credentials and force the browser and site to use those credentials.

Any ideas?

Note that this is more of an issue during testing when I need to impersonate different users.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-09-25 06:16:34

您需要做的第一件事是将代理的用户凭据写入 Windows 用户凭据缓存。

 public static void SetCredentials(string username, string password, string proxydomain)
    {
        Credential deleteCredentials = new Credential
        {
            Target = proxydomain
        };
        if (deleteCredentials.Exists())
            deleteCredentials.Delete();

        Credential proxyCredential = new Credential
        {

            Username = username,
            Password = password ,
            Target = proxydomain,
            Type = CredentialType.Generic,
            PersistanceType = PersistanceType.Enterprise
        };
        proxyCredential.Save();


    }

然后您需要将信息添加到注册表中。

 public static void setProxyRegistry(string proxyhost, bool proxyEnabled, string username, string password)
    {
        const string userRoot = "HKEY_CURRENT_USER";
        const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
        const string keyName = userRoot + "\\" + subkey;

        Registry.SetValue(keyName, "ProxyServer", proxyhost, RegistryValueKind.String);
        Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);

        if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
        {
            Registry.SetValue(keyName, "ProxyPass", password, RegistryValueKind.String);
            Registry.SetValue(keyName, "ProxyUser", username, RegistryValueKind.String);
        }

        //<-loopback>;<local>
        Registry.SetValue(keyName, "ProxyOverride", "*.local", RegistryValueKind.String);


        // These lines implement the Interface in the beginning of program 
        // They cause the OS to refresh the settings, causing IP to realy update
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }

The first thing you need to do is write the user credentials of the proxy to windows user credentials cache.

 public static void SetCredentials(string username, string password, string proxydomain)
    {
        Credential deleteCredentials = new Credential
        {
            Target = proxydomain
        };
        if (deleteCredentials.Exists())
            deleteCredentials.Delete();

        Credential proxyCredential = new Credential
        {

            Username = username,
            Password = password ,
            Target = proxydomain,
            Type = CredentialType.Generic,
            PersistanceType = PersistanceType.Enterprise
        };
        proxyCredential.Save();


    }

then you need to add the info to the registry.

 public static void setProxyRegistry(string proxyhost, bool proxyEnabled, string username, string password)
    {
        const string userRoot = "HKEY_CURRENT_USER";
        const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
        const string keyName = userRoot + "\\" + subkey;

        Registry.SetValue(keyName, "ProxyServer", proxyhost, RegistryValueKind.String);
        Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);

        if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
        {
            Registry.SetValue(keyName, "ProxyPass", password, RegistryValueKind.String);
            Registry.SetValue(keyName, "ProxyUser", username, RegistryValueKind.String);
        }

        //<-loopback>;<local>
        Registry.SetValue(keyName, "ProxyOverride", "*.local", RegistryValueKind.String);


        // These lines implement the Interface in the beginning of program 
        // They cause the OS to refresh the settings, causing IP to realy update
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文