开发者

How do I force my IE-based webbrowser control to use specific authentication credentials?

I have a WPF C# client app with an embedded webbrowser c开发者_JAVA百科ontrol. 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.


Use Kerr Credentials


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜