using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Runtime.InteropServices; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CKS.FormsBasedAuthentication.HIP { /// A visual Reverse Turing Test (HIP). [ToolboxBitmap(typeof(ImageHipChallenge), "msdn.bmp")] [ToolboxData("<{0}:ImageHipChallenge Runat=\"server\" Height=\"100px\" Width=\"300px\" />")] public class ImageHipChallenge : HipChallenge { /// Default value for the RenderUrl property. private const string RENDERURL_DEFAULT = "/_layouts/15/FBA/ImageHipChallenge.ashx"; /// Query string key for the image width. internal const string WIDTH_KEY = "w"; /// Query string key for the image height. internal const string HEIGHT_KEY = "h"; /// Query string key for challenge ID. internal const string ID_KEY = "id"; /// The dynamically-generated image. private System.Web.UI.WebControls.Image _image; /// Backing store for RenderUrl. private string _renderUrl = RENDERURL_DEFAULT; /// Gets or sets the URL used to render the image to the client. [Category("Behavior")] [Description("The URL used to render the image to the client.")] [DefaultValue(RENDERURL_DEFAULT)] public string RenderUrl { get { return _renderUrl; } set { _renderUrl = value; } } /// Creates the DynamicImage and HiddenField controls. protected sealed override void CreateChildControls() { // We're based on BaseValidator/Label, so make sure to render child controls, // though most likely no additional controls will be created. base.CreateChildControls(); // Make sure that the size of this control has been properly defined. // We need the size in pixels in order to properly generate an image. if (this.Width.IsEmpty || this.Width.Type != UnitType.Pixel || this.Height.IsEmpty || this.Height.Type != UnitType.Pixel) { throw new InvalidOperationException("Must specify size of control in pixels."); } // Create and configure the dynamic image. We won't setup the actual // Bitmap for it until later. _image = new System.Web.UI.WebControls.Image(); _image.BorderColor = this.BorderColor; _image.BorderStyle = this.BorderStyle; _image.BorderWidth = this.BorderWidth; _image.ToolTip = this.ToolTip; _image.EnableViewState = false; Controls.Add(_image); } /// Render the challenge. /// The ID of the challenge. /// The content to render. protected sealed override void RenderChallenge(Guid id, string content) { // Generate the link to the image generation handler _image.Width = this.Width; _image.Height = this.Height; _image.ImageUrl = _renderUrl + "?" + WIDTH_KEY + "=" + (int)Width.Value + "&" + HEIGHT_KEY + "=" + (int)Height.Value + "&" + ID_KEY + "=" + id.ToString("N"); } } }