Esempio n. 1
0
        public static void SetCurrentDirectory()
        {
            try
            {
                // Check if physical path was provided by ANCM
                var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
                if (string.IsNullOrEmpty(sitePhysicalPath))
                {
                    // Skip if not running ANCM InProcess
                    if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
                    {
                        return;
                    }

                    IISConfigurationData configurationData = default(IISConfigurationData);
                    if (http_get_application_properties(ref configurationData) != 0)
                    {
                        return;
                    }

                    sitePhysicalPath = configurationData.pwzFullApplicationPath;
                }

                Environment.CurrentDirectory = sitePhysicalPath;
            }
            catch
            {
                // ignore
            }
        }
        public static IISConfigurationData HttpGetApplicationProperties()
        {
            var iisConfigurationData = new IISConfigurationData();

            Validate(http_get_application_properties(ref iisConfigurationData));
            return(iisConfigurationData);
        }
Esempio n. 3
0
 public Marshaller(IISConfigurationData managed)
 {
     _native.pNativeApplication        = managed.pNativeApplication;
     _native.pwzFullApplicationPath    = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath);
     _native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath);
     _native.fWindowsAuthEnabled       = managed.fWindowsAuthEnabled ? 1 : 0;
     _native.fBasicAuthEnabled         = managed.fBasicAuthEnabled ? 1 : 0;
     _native.fAnonymousAuthEnable      = managed.fAnonymousAuthEnable ? 1 : 0;
     _native.pwzBindings        = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings);
     _native.maxRequestBodySize = managed.maxRequestBodySize;
 }
Esempio n. 4
0
        public static Native ConvertToUnmanaged(IISConfigurationData managed)
        {
            Native native;

            native.pNativeApplication        = managed.pNativeApplication;
            native.pwzFullApplicationPath    = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath);
            native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath);
            native.fWindowsAuthEnabled       = managed.fWindowsAuthEnabled ? 1 : 0;
            native.fBasicAuthEnabled         = managed.fBasicAuthEnabled ? 1 : 0;
            native.fAnonymousAuthEnable      = managed.fAnonymousAuthEnable ? 1 : 0;
            native.pwzBindings        = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings);
            native.maxRequestBodySize = managed.maxRequestBodySize;
            return(native);
        }
Esempio n. 5
0
 private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
Esempio n. 6
0
        /// <summary>
        /// Configures the port and base path the server should listen on when running behind AspNetCoreModule.
        /// The app will also be configured to capture startup errors.
        /// </summary>
        /// <param name="hostBuilder"></param>
        /// <returns></returns>
        public static IWebHostBuilder UseIISIntegration(this IWebHostBuilder hostBuilder)
        {
            if (hostBuilder == null)
            {
                throw new ArgumentNullException(nameof(hostBuilder));
            }

            // Check if `UseIISIntegration` was called already
            if (hostBuilder.GetSetting(nameof(UseIISIntegration)) != null)
            {
                return(hostBuilder);
            }

            // Check if in process
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && NativeMethods.is_ancm_loaded())
            {
                hostBuilder.UseSetting(nameof(UseIISIntegration), "true");
                hostBuilder.CaptureStartupErrors(true);

                // TODO consider adding a configuration load where all variables needed are loaded from ANCM in one call.
                var iisConfigData = new IISConfigurationData();
                var hResult       = NativeMethods.http_get_application_properties(ref iisConfigData);

                var exception = Marshal.GetExceptionForHR(hResult);
                if (exception != null)
                {
                    throw exception;
                }

                hostBuilder.UseContentRoot(iisConfigData.pwzFullApplicationPath);
                return(hostBuilder.ConfigureServices(services =>
                {
                    services.AddSingleton <IServer, IISHttpServer>();
                    services.AddSingleton <IStartupFilter>(new IISServerSetupFilter(iisConfigData.pwzVirtualApplicationPath));
                    services.AddAuthenticationCore();
                    services.Configure <IISOptions>(
                        options =>
                    {
                        options.ForwardWindowsAuthentication = iisConfigData.fWindowsAuthEnabled || iisConfigData.fBasicAuthEnabled;
                    }
                        );
                }));
            }

            var port                = hostBuilder.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}");
            var path                = hostBuilder.GetSetting(ServerPath) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPath}");
            var pairingToken        = hostBuilder.GetSetting(PairingToken) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{PairingToken}");
            var iisAuth             = hostBuilder.GetSetting(IISAuth) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{IISAuth}");
            var websocketsSupported = hostBuilder.GetSetting(IISWebSockets) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{IISWebSockets}");

            bool isWebSocketsSupported;

            if (!bool.TryParse(websocketsSupported, out isWebSocketsSupported))
            {
                // If the websocket support variable is not set, we will always fallback to assuming websockets are enabled.
                isWebSocketsSupported = (Environment.OSVersion.Version >= new Version(6, 2));
            }

            if (!string.IsNullOrEmpty(port) && !string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(pairingToken))
            {
                // Set flag to prevent double service configuration
                hostBuilder.UseSetting(nameof(UseIISIntegration), true.ToString());

                var enableAuth = false;
                if (string.IsNullOrEmpty(iisAuth))
                {
                    // back compat with older ANCM versions
                    enableAuth = true;
                }
                else
                {
                    // Lightup a new ANCM variable that tells us if auth is enabled.
                    foreach (var authType in iisAuth.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (!string.Equals(authType, "anonymous", StringComparison.OrdinalIgnoreCase))
                        {
                            enableAuth = true;
                            break;
                        }
                    }
                }

                var address = "http://127.0.0.1:" + port;
                hostBuilder.CaptureStartupErrors(true);

                hostBuilder.ConfigureServices(services =>
                {
                    // Delay register the url so users don't accidently overwrite it.
                    hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, address);
                    hostBuilder.PreferHostingUrls(true);
                    services.AddSingleton <IStartupFilter>(new IISSetupFilter(pairingToken, new PathString(path), isWebSocketsSupported));
                    services.Configure <ForwardedHeadersOptions>(options =>
                    {
                        options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
                    });
                    services.Configure <IISOptions>(options =>
                    {
                        options.ForwardWindowsAuthentication = enableAuth;
                    });
                    services.AddAuthenticationCore();
                });
            }

            return(hostBuilder);
        }