C# webBrowser自动登陆 windows集成验证的网站

本文引用了 翼帆 网友的一文:

http://www.cnblogs.com/gsyifan/archive/2010/08/27/1810022.html 

目前很多的网站的访问方式是与Window验证集成的,需要输入争取的用户名密码,才能完成登录。

面对这样的网站我们如何采用WebBrowser来实现自动登录是个难题。

下面本文就将来解决这一问题 

开发环境Vs2005

 

源码下载

 

 

1:创建控件:

button1 

webBrowser1 

textBox1  网址

textBox2  用户名

textBox3  密码

 2:

using...

 

 1 using System;
 2 using System.Windows.Forms;
 3 using System.Runtime.InteropServices;

 

 

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WebBrowser登陆集成windows验证
{
    
#region COM Interfaces
  
          [ComImport,
          Guid(
"00000112-0000-0000-C000-000000000046"),
          InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
          
public interface IOleObject
          {
              
void SetClientSite(IOleClientSite pClientSite);
              
void GetClientSite(IOleClientSite ppClientSite);
              
void SetHostNames(object szContainerApp, object szContainerObj);
              
void Close(uint dwSaveOption);
              
void SetMoniker(uint dwWhichMoniker, object pmk);
              
void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
              
void InitFromData(IDataObject pDataObject, bool
              fCreation, 
uint dwReserved);
              
void GetClipboardData(uint dwReserved, IDataObject ppDataObject);
              
void DoVerb(uint iVerb, uint lpmsg, object pActiveSite,
              
uint lindex, uint hwndParent, uint lprcPosRect);
              
void EnumVerbs(object ppEnumOleVerb);
              
void Update();
              
void IsUpToDate();
              
void GetUserClassID(uint pClsid);
              
void GetUserType(uint dwFormOfType, uint pszUserType);
              
void SetExtent(uint dwDrawAspect, uint psizel);
              
void GetExtent(uint dwDrawAspect, uint psizel);
              
void Advise(object pAdvSink, uint pdwConnection);
              
void Unadvise(uint dwConnection);
              
void EnumAdvise(object ppenumAdvise);
              
void GetMiscStatus(uint dwAspect, uint pdwStatus);
              
void SetColorScheme(object pLogpal);
          }
  
          [ComImport,
          Guid(
"00000118-0000-0000-C000-000000000046"),
          InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
          
public interface IOleClientSite
          {
              
void SaveObject();
              
void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
              
void GetContainer(object ppContainer);
              
void ShowObject();
              
void OnShowWindow(bool fShow);
              
void RequestNewObjectLayout();
          }
  
          [ComImport,
          GuidAttribute(
"6d5140c1-7436-11ce-8034-00aa006009fa"),
          InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
          ComVisible(
false)]
          
public interface IServiceProvider
          {
              [
return: MarshalAs(UnmanagedType.I4)]
              [PreserveSig]
              
int QueryService(ref Guid guidService, ref Guid riid, out IntPtr
              ppvObject);
          }
  
          [ComImport, GuidAttribute(
"79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"),
          InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
          ComVisible(
false)]
          
public interface IAuthenticate
          {
              [
return: MarshalAs(UnmanagedType.I4)]
              [PreserveSig]
              
int Authenticate(ref IntPtr phwnd,
              
ref IntPtr pszUsername,
              
ref IntPtr pszPassword
              );
          }
  
          
#endregion
    
public partial class Form1 : Form, IOleClientSite, IServiceProvider, IAuthenticate
    {
        
public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b");
           
public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
          
public const int S_OK = unchecked((int)0x00000000);
        
public Form1()
        {
            InitializeComponent();
        }

        
#region IOleClientSite 成员

        
public void SaveObject()
        {
            
throw new Exception("The method or operation is not implemented.");
        }

        
public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk)
        {
            
throw new Exception("The method or operation is not implemented.");
        }

        
public void GetContainer(object ppContainer)
        {
            ppContainer 
= this;
        }

        
public void ShowObject()
        {
            
throw new Exception("The method or operation is not implemented.");
        }

        
public void OnShowWindow(bool fShow)
        {
            
throw new Exception("The method or operation is not implemented.");
        }

        
public void RequestNewObjectLayout()
        {
            
throw new Exception("The method or operation is not implemented.");
        }

        
#endregion

        
#region IServiceProvider 成员

        
public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
        {
            
int nRet = guidService.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
              if (nRet == 0)
             {
                 nRet 
= riid.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
                 if (nRet == 0)
                 {
                     ppvObject 
= Marshal.GetComInterfaceForObject(this,
                     
typeof(IAuthenticate));
                     
return S_OK;
                 }
             }
             ppvObject 
= new IntPtr();
             
return INET_E_DEFAULT_ACTION;
        }

        
#endregion

        
#region IAuthenticate 成员

        
public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername, ref IntPtr pszPassword)
        {
            IntPtr sUser 
= Marshal.StringToCoTaskMemAuto(textBox2.Text);
            IntPtr sPassword 
= Marshal.StringToCoTaskMemAuto(textBox3.Text);
 
             pszUsername 
= sUser;
             pszPassword 
= sPassword;
             
return S_OK;
        }

        
#endregion

        
private void button1_Click(object sender, EventArgs e)
        {
            
string oURL = this.textBox1.Text;
              webBrowser1.Navigate(oURL);
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            
string oURL = "about:blank";
            webBrowser1.Navigate(oURL);

            
object obj = webBrowser1.ActiveXInstance;
            IOleObject oc 
= obj as IOleObject;
             oc.SetClientSite(
this as IOleClientSite);
        }
    }
}

 

 

posted @ 2010-08-27 16:02  北方的狼  Views(1734)  Comments(0Edit  收藏  举报