2011-07-26 66 views
1

我需要爲我的C#應用​​程序中的託管WebBrowser控件創建自定義安全管理器。託管的WebBrowser控件應獨立於IE安全和隱私設置。在Webbrowser控件中使用IInternetSecurityManager覆蓋IE隱私設置

問題:如果IE隱私設置設置爲「高」,那麼依靠cookie的網站在託管的WebBrowser控件中無法正常工作(如果隱私設置設置爲「中」,則網站可以正常工作)。

託管WebBrowser控件的winform爲GetSecurityId,MapUrlToZone和ProcessUrlAction提供了以下實現。 ProcessUrlAction被嵌入式瀏覽器調用,用於諸如ACTIVEX_RUN,SCRIPT_RUN等URL操作;但它沒有被調用任何與cookie相關的url操作。

有沒有一些方法來啓用瀏覽器控制cookies?或者下面的代碼有問題嗎?

private void InitBrowserSecurityData() 
{ 
    securityID = new byte[MAX_SIZE_SECURITY_ID]; 

    securityID[0] = (byte)'f'; 
    securityID[1] = (byte)'i'; 
    securityID[2] = (byte)'l'; 
    securityID[3] = (byte)'e'; 
    securityID[4] = (byte)':'; 
    securityID[5] = 0; 
    securityID[6] = 3; 
    securityID[7] = 0; 
    securityID[8] = 0; 
    securityID[9] = 0; 
} 

//GetSecurityId 
int IInternetSecurityManager.GetSecurityId(string pwszUrl, IntPtr pbSecurityId, ref uint pcbSecurityId, ref uint dwReserved) 
{ 
    if (pcbSecurityId >= MAX_SIZE_SECURITY_ID) 
    { 
     Marshal.Copy(securityID, 0, pbSecurityId, MAX_SIZE_SECURITY_ID); 
     pcbSecurityId = 9; 
     return HResults.S_OK; 
    } 
    return (int)WinInetErrors.INET_E_DEFAULT_ACTION; 
} 

//MapUrlToZone 
int IInternetSecurityManager.MapUrlToZone(string pwszUrl, out uint pdwZone, uint dwFlags) 
{ 
    pdwZone = (uint)tagURLZONE.URLZONE_LOCAL_MACHINE; 
    return HResults.S_OK; 
} 

//ProcessUrlAction 
int IInternetSecurityManager.ProcessUrlAction(string pwszUrl, uint dwAction, IntPtr pPolicy, 
    uint cbPolicy, IntPtr pContext, uint cbContext, uint dwFlags, uint dwReserved) 
{ 
    URLACTION action = (URLACTION)dwAction; 

    bool hasUrlPolicy = (cbPolicy >= unchecked((uint)Marshal.SizeOf(typeof(int)))); 
    URLPOLICY urlPolicy = (hasUrlPolicy) ? (URLPOLICY)Marshal.ReadInt32(pPolicy) : URLPOLICY.ALLOW; 

    bool hasContext = (cbContext >= unchecked((uint)Marshal.SizeOf(typeof(Guid)))); 
    Guid context = (hasContext) ? (Guid)Marshal.PtrToStructure(pContext, typeof(Guid)) : Guid.Empty; 
    //all URL actions are allowed 
    if (hasUrlPolicy) 
    { 
     urlPolicy = URLPOLICY.ALLOW; 
     Marshal.WriteInt32(pPolicy, (int)urlPolicy); 
     return HResults.S_OK; 
    } 
    return (int)WinInetErrors.INET_E_DEFAULT_ACTION; 
} 

我已經看到了這些問題,但他們不適合我。

Security Level for WebBrowser control

Custom IInternetSecurityManager not being called with dialogs

overriding GetSecurityId in IInternetSecurityManager

+0

我有類似的問題。你的問題能解決嗎? –

+0

對不起,這個遲到的回覆Birk。看起來Cookie的行爲永遠不會傳遞給IInternetSecurityManager ...看看[this](http://www.tech-archive.net/Archive/InetSDK/microsoft.public.inetsdk.programming.webbrowser_ctl/2006-03/ msg00073.html) – CPJoshi

回答

1

看來,Internet Explorer不會通過Cookie的行動IInternetSecurityManager。這就是很難在嵌入式瀏覽器控件中覆蓋IE隱私設置的原因。有些明智的人已經很長時間地想到了這一點here