C# webBrowser自動登陸windows整合驗證方法

來源:互聯網
上載者:User
1:建立控制項:button1 webBrowser1 textBox1  網址textBox2  使用者名稱textBox3  密碼 2:using...  1 using System; 2 using System.Windows.Forms; 3 using System.Runtime.InteropServices;  3:完整代碼。   1   2 namespace WebTest1  3 {  4     #region COM Interfaces  5   6         [ComImport,  7         Guid("00000112-0000-0000-C000-000000000046"),  8         InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  9         public interface IOleObject 10         { 11             void SetClientSite(IOleClientSite pClientSite); 12             void GetClientSite(IOleClientSite ppClientSite); 13             void SetHostNames(object szContainerApp, object szContainerObj); 14             void Close(uint dwSaveOption); 15             void SetMoniker(uint dwWhichMoniker, object pmk); 16             void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk); 17             void InitFromData(IDataObject pDataObject, bool 18             fCreation, uint dwReserved); 19             void GetClipboardData(uint dwReserved, IDataObject ppDataObject); 20             void DoVerb(uint iVerb, uint lpmsg, object pActiveSite, 21             uint lindex, uint hwndParent, uint lprcPosRect); 22             void EnumVerbs(object ppEnumOleVerb); 23             void Update(); 24             void IsUpToDate(); 25             void GetUserClassID(uint pClsid); 26             void GetUserType(uint dwFormOfType, uint pszUserType); 27             void SetExtent(uint dwDrawAspect, uint psizel); 28             void GetExtent(uint dwDrawAspect, uint psizel); 29             void Advise(object pAdvSink, uint pdwConnection); 30             void Unadvise(uint dwConnection); 31             void EnumAdvise(object ppenumAdvise); 32             void GetMiscStatus(uint dwAspect, uint pdwStatus); 33             void SetColorScheme(object pLogpal); 34         } 35  36         [ComImport, 37         Guid("00000118-0000-0000-C000-000000000046"), 38         InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 39         public interface IOleClientSite 40         { 41             void SaveObject(); 42             void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk); 43             void GetContainer(object ppContainer); 44             void ShowObject(); 45             void OnShowWindow(bool fShow); 46             void RequestNewObjectLayout(); 47         } 48  49         [ComImport, 50         GuidAttribute("6d5140c1-7436-11ce-8034-00aa006009fa"), 51         InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), 52         ComVisible(false)] 53         public interface IServiceProvider 54         { 55             [return: MarshalAs(UnmanagedType.I4)] 56             [PreserveSig] 57             int QueryService(ref Guid guidService, ref Guid riid, out IntPtr 58             ppvObject); 59         } 60  61         [ComImport, GuidAttribute("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"), 62         InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), 63         ComVisible(false)] 64         public interface IAuthenticate 65         { 66             [return: MarshalAs(UnmanagedType.I4)] 67             [PreserveSig] 68             int Authenticate(ref IntPtr phwnd, 69             ref IntPtr pszUsername, 70             ref IntPtr pszPassword 71             ); 72         } 73  74         #endregion 75     public partial class Form1 : Form, IOleClientSite, IServiceProvider,IAuthenticate 76     { 77  78         public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b"); 79         public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011); 80         public const int S_OK = unchecked((int)0x00000000); 81  82         public Form1() 83         { 84  85             InitializeComponent(); 86         } 87  88         private void button1_Click(object sender, EventArgs e) 89         { 90             string oURL = this.textBox1.Text; 91             webBrowser1.Navigate(oURL); 92  93  94         } 95  96         private void Form1_Load(object sender, EventArgs e) 97         { 98             string oURL = "about:blank"; 99             webBrowser1.Navigate(oURL);100 101 102 103             object obj = webBrowser1.ActiveXInstance;104             IOleObject oc = obj as IOleObject;105             oc.SetClientSite(this as IOleClientSite);106         }107         #region IOleClientSite Members108 109         public void SaveObject()110         {111             // TODO: Add Form1.SaveObject implementation112         }113 114         public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object115         ppmk)116         {117             // TODO: Add Form1.GetMoniker implementation118         }119 120         public void GetContainer(object ppContainer)121         {122             ppContainer = this;123         }124 125         public void ShowObject()126         {127             // TODO: Add Form1.ShowObject implementation128         }129 130         public void OnShowWindow(bool fShow)131         {132             // TODO: Add Form1.OnShowWindow implementation133         }134 135         public void RequestNewObjectLayout()136         {137             // TODO: Add Form1.RequestNewObjectLayout implementation138         }139 140         #endregion141 142         #region IServiceProvider Members143 144         public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)145         {146             int nRet = guidService.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal147             if (nRet == 0)148             {149                 nRet = riid.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal150                 if (nRet == 0)151                 {152                     ppvObject = Marshal.GetComInterfaceForObject(this,153                     typeof(IAuthenticate));154                     return S_OK;155                 }156             }157             ppvObject = new IntPtr();158             return INET_E_DEFAULT_ACTION;159         }160 161         #endregion162 163         #region IAuthenticate Members164 165         public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername,ref IntPtr pszPassword)166         {167             IntPtr sUser = Marshal.StringToCoTaskMemAuto(textBox2.text);168             IntPtr sPassword = Marshal.StringToCoTaskMemAuto(textBox3.text);169 170             pszUsername = sUser;171             pszPassword = sPassword;172             return S_OK;173         }174 175         #endregion176 177     }178 }179 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.