using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace Nuclex.Networking.Gallery3.Requests { /// Creates requests that use HTTP authentication public class AuthenticatingRequestFactory : RequestFactory { /// Initializs a new simple request factory /// URL of the gallery installation /// /// If the URL doesn't end with 'index.php', it will automatically /// be appended to the URL. For gallery installations where index.php /// is not required in the path, use the second constructor overload. /// public AuthenticatingRequestFactory(string galleryUrl) : base(galleryUrl) { } /// Initializs a new simple request factory /// URL of the gallery installation /// /// Whether to take to URL as it is or to append 'index.php' if it is missing /// /// /// By default, gallery 3 installations require 'index.php' to be specified /// in the URL with any REST request. If you have modified your gallery 3 /// installation, renamed index.php or do not want this for any other reason, /// specifying true for the parameter makes /// the request factory use the URL exactly as it is specified. /// public AuthenticatingRequestFactory(string galleryUrl, bool asIs) : base(galleryUrl, asIs) { } /// User name with which to log in at the server public string User { get; set; } /// Password to use for logging in at the server public string Password { get; set; } /// Authentication type to use /// /// Normally either "Basic" or "Digest" depending on the server's /// authentication scheme. /// public string AuthType { get; set; } /// Called when a new WebRequest is created /// The new WebRequest /// /// This method can be overridden in derived classes to modify new requests /// created by the factory, for example in order to add authentication options. /// protected override void OnRequestCreated(System.Net.WebRequest request) { updateCredentialCache(); request.Credentials = this.credentialCache; } /// Ensures that the credential cache is up-to-date private void updateCredentialCache() { bool changed = (cachedUser != User) || (cachedPassword != Password); this.credentialCache = new CredentialCache(); this.credentialCache.Add( GalleryUri, "Basic", new NetworkCredential(User, Password) ); this.cachedUser = User; this.cachedPassword = Password; } /// Stores the credentials used to log in private CredentialCache credentialCache; /// User that is currently stored in the credential cache private string cachedUser; /// Password that is currently stored in the credential cache private string cachedPassword; } } // namespace Nuclex.Networking.Gallery3.Requests