using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace CKS.FormsBasedAuthentication.HIP
{
/// Validation control used in conjunction with an HipChallenge.
[ToolboxBitmap(typeof(HipValidator), "msdn.bmp")]
[ToolboxData("<{0}:HipValidator Runat=\"server\" ErrorMessage=\"*\" />")]
public class HipValidator : BaseValidator
{
/// Backing store for HipChallenge.
private string _hipChallenge;
/// Gets or sets the ID of the associated HipChallenge.
/// The ID of the associated HipChallenge.
[TypeConverter(typeof(HipChallengeControlConverter))]
[Category("Behavior")]
public string HipChallenge
{
get { return _hipChallenge; }
set { _hipChallenge = value; }
}
/// Gets the associated HipChallenge on the page.
/// The associated HipChallenge on the page.
private HipChallenge AssociatedChallenge
{
get
{
if (HipChallenge == null || HipChallenge.Trim().Length == 0) throw new InvalidOperationException("No challenge control specified.");
HipChallenge hip = NamingContainer.FindControl(HipChallenge) as HipChallenge;
if (hip == null) throw new InvalidOperationException("Could not find challenge control.");
return hip;
}
}
/// Determines whether this validator's source is valid.
/// Whether the validated source is valid.
protected override bool EvaluateIsValid()
{
// Get the validated control and its value. If we can get a value, see if
// it authenticates with the associated HipChallenge.
string controlName = base.ControlToValidate;
if (controlName != null)
{
string controlValue = base.GetControlValidationValue(controlName);
if (controlValue != null && ((controlValue = controlValue.Trim()).Length > 0))
{
return AssociatedChallenge.Authenticate(controlValue);
}
}
return false;
}
/// TypeConverter used to present developer with list of HipChallenge controls on page.
private class HipChallengeControlConverter : ValidatedControlConverter
{
/// Gets a list of all HipChallenge components on the page.
/// The IContainer from the designer.
/// An array of HipChallenge control IDs.
private object[] GetControls(IContainer container)
{
ArrayList list = new ArrayList();
foreach(IComponent comp in container.Components)
{
HipChallenge hip = comp as HipChallenge;
if (hip != null)
{
if (hip.ID != null && hip.ID.Trim().Length > 0) list.Add(hip.ID);
}
}
list.Sort(Comparer.Default);
return list.ToArray();
}
/// Returns a standard collection of values based on the type descriptor context.
/// The context.
/// A collection of HipChallenge control IDs.
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if (context == null || context.Container == null) return null;
object [] controls = GetControls(context.Container);
if (controls != null) return new StandardValuesCollection(controls);
return null;
}
}
}
}