public static void RequestAuthentication(string authEndpoint, Uri returnUrl, string consumerKey, RedirectMethod redirectMethod)
        {
            var builder = new UriBuilder(authEndpoint);

            builder.AppendQueryArgs(
                new Dictionary<string, string> {
                    { "client_id", consumerKey },
                    { "redirect_uri", returnUrl.AbsoluteUri },
                    { "scope", "email" },
                });

            redirectMethod(builder.Uri.AbsoluteUri, true);
        }
        /// <summary>
        /// Redirects to the currently contained URL.
        /// </summary>
        /// <param name="method">The method.</param>
        public void Redirect(
            RedirectMethod method)
        {
            HttpResponse response = HttpContext.Current.Response;

            if (method == RedirectMethod.Temporary)
            {
                response.Redirect(AllUrl, true);
            }
            else if (method == RedirectMethod.Permanent)
            {
                response.Clear();

                response.StatusCode        = 301;
                response.StatusDescription = @"Moved Permanently";

                response.RedirectLocation = AllUrl;

                response.Write(
                    @"<html><head><title>Object moved</title></head><body>" +
                    Environment.NewLine);
                response.Write(
                    string.Format(
                        @"<h2>Object moved to <a href=""{0}"">here</a>.</h2>" +
                        Environment.NewLine,
                        HttpUtility.HtmlAttributeEncode(AllUrl)));
                response.Write(
                    @"</body></html>" +
                    Environment.NewLine);

                response.End();
            }
            else
            {
                throw new ArgumentException(
                          string.Format(
                              @"Unknown redirect method '{0}'.",
                              method),
                          @"method");
            }
        }
        public static void RequestAuthentication(string authEndpoint, Uri returnUrl, string consumerKey, RedirectMethod redirectMethod)
        {
            var builder = new UriBuilder(authEndpoint);

            builder.AppendQueryArgs(
                new Dictionary <string, string> {
                { "client_id", consumerKey },
                { "redirect_uri", returnUrl.AbsoluteUri },
                { "scope", "email" },
            });

            redirectMethod(builder.Uri.AbsoluteUri, true);
        }
        /// <summary>
        /// The main entry point for the redirector
        /// </summary>
        /// <returns>Returns wether the DNS cache results match the selected IPAddresses</returns>
        public static bool Initialize()
        {
            // Only initialize once
            if (!IsInitialized)
            {
                IsInitialized = true;
                TraceLog.WriteLine("Initializing Redirector");
                TraceLog.Indent();

                // Set the System.Net DNS Cache refresh timeout to 1 millisecond
                ServicePointManager.DnsRefreshTimeout = 1;

                // Get config options
                RedirectMethod = Program.Config.RedirectMode;
                TraceLog.WriteLine("Chosen Redirect mode: " + RedirectMethod.ToString());

                // Create new Instances
                HostsFileSys = new SysHostsFile();
                HostsFileIcs = new HostsFileIcs();

                // Detect redirects
                bool IcsHasRedirects   = HostsFileIcs.HasAnyEntry(GamespyHosts) || HostsFileIcs.HasEntry(Bf2StatsHost);
                bool HostsHasRedirects = HostsFileSys.HasAnyEntry(GamespyHosts) || HostsFileSys.HasEntry(Bf2StatsHost);

                // Write tracelogs
                TraceLog.WriteLine("System Hosts has redirects: " + ((HostsHasRedirects) ? "True" : "False"));
                TraceLog.WriteLine("Hosts.ics has redirects: " + ((IcsHasRedirects) ? "True" : "False"));

                // Both files cannot have redirects!
                if (IcsHasRedirects && HostsHasRedirects)
                {
                    // Get the Non-Selected mode, and remove those redirects
                    HostsFile toRemove = (RedirectMethod == RedirectMode.HostsFile)
                        ? HostsFileIcs as HostsFile
                        : HostsFileSys as HostsFile;

                    try
                    {
                        // Remove all redirects
                        TraceLog.Write("Removing redirects from unchosen hostsfile... ");
                        RemoveRedirects(toRemove);
                        TraceLog.WriteLine("Success");
                    }
                    catch (Exception e)
                    {
                        TraceLog.WriteLine("Failed!");
                        TraceLog.Indent();
                        TraceLog.TraceError(e.Message);
                    }
                }

                // Set old redirect data if we have it
                if (RedirectsEnabled)
                {
                    // Grab our service provider
                    ServiceProvider provider = ClientSettings.ServiceProviders
                                               .Where(x => x.Name == Program.Config.LastUsedProvider)
                                               .FirstOrDefault();

                    // Make sure we have an object before settings
                    if (provider != null)
                    {
                        SetProviderIPAddress(provider);
                    }
                }

                // Remove all indents
                TraceLog.Unindent(true);
            }

            // Verify cache
            return((RedirectsEnabled) ? VerifyDNSCache() : true);
        }