상속: System.EventArgs
예제 #1
0
        public bool GetCredentials(GetCredentialsEventArgs args)
        {
            try
            {
                //check if this game has some stored details
                if (SettingsManager.Instance.Settings == null)
                {
                    //if not, create some
                    SettingsManager.Instance.Settings = new GameSettings { GameUid = _game.Uid, };
                }

                args.UserName = SettingsManager.Instance.Settings.UserName;
                args.Password = SettingsManager.Instance.Settings.Password;

                //if we have no credentials or we have incorrect stored credentials
                if (string.IsNullOrEmpty(args.UserName) || _requiresManualIntervention)
                {
                    if (_newCredentials(args))
                    {
                        SettingsManager.Instance.Settings.UserName = args.UserName;
                        SettingsManager.Instance.Settings.Password = args.Password;
                        SettingsManager.Instance.Save();
                        return true;
                    }
                    else
                    {
                        SettingsManager.Instance.Settings.UserName = string.Empty;
                        SettingsManager.Instance.Settings.Password = string.Empty;
                        SettingsManager.Instance.Save();
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            finally
            {
                //if this callback is called again, it must mean that the
                //credentials supplied were wrong so must be entered manually
                _requiresManualIntervention = true;
            }
        }
예제 #2
0
        public Stream GetResponseStream(string uri, long progress, Func<GetCredentialsEventArgs, bool> getCredentials, out long contentLength)
        {
            var response = _responses[uri];

            if (response==null)
            {
                throw new WebException("404 File not found");
            }

            if (_credentials.ContainsKey(uri))
            {
                bool hasValidCredentials = false;
                if (getCredentials != null)
                {
                    var args = new GetCredentialsEventArgs();
                    while (getCredentials(args))
                    {
                        if (_credentials[uri].Key==args.UserName && _credentials[uri].Value==args.Password)
                        {
                            hasValidCredentials = true;
                            break;
                        }
                    }
                }

                if (!hasValidCredentials)
                {
                    throw new WebException("401 Unauthorized");
                }
            }
            contentLength = response.Data.Length;
            var stream = new MockStream(response.Data);
            if (response.OnRead != null) stream.OnRead += response.OnRead;
            if (response.OnWrite != null) stream.OnWrite += response.OnWrite;
            stream.Open();
            stream.Seek(progress, SeekOrigin.Begin);
            return stream;
        }
예제 #3
0
        public Stream GetResponseStream(string uri,long progress, Func<GetCredentialsEventArgs,bool> getCredentials,out long contentLength)
        {
            var credentials = new GetCredentialsEventArgs();
            bool requiresAuthentication = false;

            const int maxRedirects = 5;
            int redirects = 0;
            do
            {
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.AllowAutoRedirect = false;

                if (requiresAuthentication)
                {
                    var cache = new CredentialCache {
                        { new Uri(uri, UriKind.Absolute), "Basic", new NetworkCredential(credentials.UserName, credentials.Password) },
                        { new Uri(uri, UriKind.Absolute), "Digest", new NetworkCredential(credentials.UserName, credentials.Password) }
                    };
                    request.Credentials = cache;
                }
                else
                {
                    request.Credentials = CredentialCache.DefaultCredentials;
                }

                if (progress>0) AddRange(progress, request);

                try
                {
                    var response = (HttpWebResponse) request.GetResponse();

                    if (response.StatusCode == HttpStatusCode.Found)
                    {
                        uri = response.Headers["Location"];
                    }
                    else
                    {
                        contentLength = response.ContentLength;
                        return response.GetResponseStream();
                    }
                }
                catch (WebException ex)
                {
                    bool rethrow = true;

                    if (ex.Response!=null)
                    {
                        HttpWebResponse errorResponse = (HttpWebResponse) ex.Response;
                        if (errorResponse.StatusCode == HttpStatusCode.Unauthorized && getCredentials!=null && getCredentials(credentials))
                        {
                            //retry the request with the credentials supplied from the user, for all other errors
                            //just rethrow the exception.
                            requiresAuthentication = true;
                            rethrow = false;
                        }
                    }

                    if (rethrow) throw ex;
                }
            }
            while (redirects++<maxRedirects);

            throw new Exception("Maximum redirect count exceeded");
        }
예제 #4
0
        public Stream GetResponseStream(string uri, long progress, Func <GetCredentialsEventArgs, bool> getCredentials, out long contentLength)
        {
            var  credentials            = new GetCredentialsEventArgs();
            bool requiresAuthentication = false;

            const int maxRedirects = 5;
            int       redirects    = 0;

            do
            {
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.AllowAutoRedirect = false;

                if (requiresAuthentication)
                {
                    var cache = new CredentialCache {
                        { new Uri(uri, UriKind.Absolute), "Basic", new NetworkCredential(credentials.UserName, credentials.Password) },
                        { new Uri(uri, UriKind.Absolute), "Digest", new NetworkCredential(credentials.UserName, credentials.Password) }
                    };
                    request.Credentials = cache;
                }
                else
                {
                    request.Credentials = CredentialCache.DefaultCredentials;
                }

                if (progress > 0)
                {
                    AddRange(progress, request);
                }

                try
                {
                    var response = (HttpWebResponse)request.GetResponse();

                    if (response.StatusCode == HttpStatusCode.Found)
                    {
                        uri = response.Headers["Location"];
                    }
                    else
                    {
                        contentLength = response.ContentLength;
                        return(response.GetResponseStream());
                    }
                }
                catch (WebException ex)
                {
                    bool rethrow = true;

                    if (ex.Response != null)
                    {
                        HttpWebResponse errorResponse = (HttpWebResponse)ex.Response;
                        if (errorResponse.StatusCode == HttpStatusCode.Unauthorized && getCredentials != null && getCredentials(credentials))
                        {
                            //retry the request with the credentials supplied from the user, for all other errors
                            //just rethrow the exception.
                            requiresAuthentication = true;
                            rethrow = false;
                        }
                    }

                    if (rethrow)
                    {
                        throw ex;
                    }
                }
            }while (redirects++ < maxRedirects);

            throw new Exception("Maximum redirect count exceeded");
        }