Exemple #1
0
        /// <summary>
        /// Map the file to SecurityZone using urlmon.dll, depending on 'IInternetSecurityManager::MapUrlToZone'.
        /// </summary>
        private static SecurityZone MapSecurityZoneWithUrlmon(string filePath)
        {
            uint         zoneId;
            object       curSecMgr           = null;
            const UInt32 MUTZ_DONT_USE_CACHE = 0x00001000;

            int hr = NativeMethods.CoInternetCreateSecurityManager(null, out curSecMgr, 0);

            if (hr != NativeMethods.S_OK)
            {
                // Returns an error value if it's not S_OK
                throw new System.ComponentModel.Win32Exception(hr);
            }

            try
            {
                NativeMethods.IInternetSecurityManager ism = (NativeMethods.IInternetSecurityManager)curSecMgr;
                hr = ism.MapUrlToZone(filePath, out zoneId, MUTZ_DONT_USE_CACHE);
                if (hr == NativeMethods.S_OK)
                {
                    SecurityZone result;
                    return(LanguagePrimitives.TryConvertTo(zoneId, out result) ? result : SecurityZone.NoZone);
                }
                return(SecurityZone.NoZone);
            }
            finally
            {
                if (curSecMgr != null)
                {
                    Marshal.ReleaseComObject(curSecMgr);
                }
            }
        }
Exemple #2
0
        internal static int MapUrlToZoneWrapper(Uri uri)
        {
            int    targetZone = NativeMethods.URLZONE_LOCAL_MACHINE; // fail securely this is the most priveleged zone
            int    hr         = NativeMethods.S_OK;
            object curSecMgr  = null;

            hr = NativeMethods.CoInternetCreateSecurityManager(
                null,
                out curSecMgr,
                0);
            if (NativeMethods.Failed(hr))
            {
                throw new Win32Exception(hr);
            }

            NativeMethods.IInternetSecurityManager pSec = (NativeMethods.IInternetSecurityManager)curSecMgr;

            string uriString = BindUriHelper.UriToString(uri);

            //
            // special case the condition if file is on local machine or UNC to ensure that content with mark of the web
            // does not yield with an internet zone result
            //
            if (uri.IsFile)
            {
                pSec.MapUrlToZone(uriString, out targetZone, NativeMethods.MUTZ_NOSAVEDFILECHECK);
            }
            else
            {
                pSec.MapUrlToZone(uriString, out targetZone, 0);
            }
            //
            // This is the condition for Invalid zone
            //
            if (targetZone < 0)
            {
                throw new SecurityException("The URI specified is invalid.");
            }
            pSec      = null;
            curSecMgr = null;
            return(targetZone);
        }