Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 20th, 2013  |  syntax: None  |  size: 6.59 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public interface IHttpHeaderInspectingAuthenticatorFactory<T>
  2.     where T: HttpHeaderInspectingAuthenticatorConfigurationElement
  3. {
  4.     IHttpHeaderInspectingAuthenticator<T> Construct(T config);
  5. }
  6.  
  7. public class BasicAuthenticationInspectingAuthenticatorFactory :
  8.     IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement>
  9. {
  10.     public IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement> Construct(BasicAuthenticationHeaderInspectorConfigurationElement config)
  11.     {
  12.         return new BasicAuthenticationInspectingAuthenticator(config);
  13.     }
  14. }
  15.  
  16. public class BasicAuthenticationInspectingAuthenticator : HttpHeaderInspectingAuthenticatorBase<BasicAuthenticationHeaderInspectorConfigurationElement>
  17. {
  18.     internal BasicAuthenticationInspectingAuthenticator(BasicAuthenticationHeaderInspectorConfigurationElement config)
  19.         : base(config) {}
  20.  
  21.  //snip -- IHttpHeaderInspectingAuthenticator<T> and IHttpHeaderInspectingAuthenticator implementation
  22. }
  23.  
  24.  
  25. public class BasicAuthenticationHeaderInspectorConfigurationElement : HttpHeaderInspectingAuthenticatorConfigurationElement
  26. {
  27.  //extra properties
  28. }
  29.  
  30.  
  31. public class HttpHeaderInspectingAuthenticatorConfigurationElement : ConfigurationElement
  32. {
  33.  protected override void PostDeserialize()
  34.  {
  35.      base.PostDeserialize();
  36.  
  37.      //simple verification of info supplied in config
  38.      var t = Type.GetType(Factory);
  39.      if (null == t)
  40.          throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] cannot be found - check configuration settings", Factory ?? ""));
  41.  
  42.      if (!typeof(IHttpHeaderInspectingAuthenticatorFactory<>).IsGenericInterfaceAssignableFrom(t))
  43.          throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] must derive from {1} - check configuration settings", Factory ?? "", typeof(IHttpHeaderInspectingAuthenticatorFactory<>).Name));
  44.  
  45.      var c = t.GetConstructor(Type.EmptyTypes);
  46.      if (null == c)
  47.          throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] must have a parameterless constructor - check configuration settings", Factory ?? ""));
  48.  }
  49.  
  50.  [ConfigurationProperty("factory", IsRequired = true)]
  51.  public string Factory
  52.  {
  53.   get { return (string)this["factory"]; }
  54.   set { this["factory"] = value; }
  55.  }
  56.  
  57.  //this allows us to use types derived from HttpHeaderInspectingAuthenticatorConfigurationElement
  58.     protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
  59.     {                    
  60.         ConfigurationProperty property = new ConfigurationProperty(name, typeof(string), value);
  61.         Properties.Add(property);
  62.         base[property] = value;            
  63.         return true;
  64.     }
  65.  
  66.  public IHttpHeaderInspectingAuthenticator GetInspector()
  67.  {
  68.      dynamic factoryInstance = Activator.CreateInstance(Type.GetType(Factory));
  69.      return factoryInstance.Construct(this);
  70.  }
  71. }
  72.        
  73. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'Redcated.Authentication.BasicAuthenticationInspectingAuthenticatorFactory.Construct(Redcated.Authentication.Configuration.BasicAuthenticationHeaderInspectorConfigurationElement)' has some invalid arguments
  74.   at CallSite.Target(Closure , CallSite , Object , HttpHeaderInspectingAuthenticatorConfigurationElement )
  75.   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
  76.   at Redcated.Authentication.Configuration.HttpHeaderInspectingAuthenticatorConfigurationElement.GetInspector() in D:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryConfigurationHttpHeaderInspectingAuthenticatorConfigurationElement.cs:line 79
  77.   at Redcated.Authentication.Configuration.HttpHeaderInspectingAuthenticatorConfigurationElementCollection.<GetInspectors>b__0(HttpHeaderInspectingAuthenticatorConfigurationElement i) in D:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryConfigurationHttpHeaderInspectingAuthenticatorConfigurationElementCollection.cs:line 78
  78.   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  79.   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
  80.   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
  81.   at Redcated.Authentication.HttpHeaderInspectingAuthenticationModule.AuthenticateRequest(Object sender, EventArgs e) in D:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryHttpHeaderInspectingAuthenticationModule.cs:line 49
  82.   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  83.   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
  84.        
  85. public interface IHttpHeaderInspectingAuthenticatorFactory
  86. {
  87.     IHttpHeaderInspectingAuthenticator Construct(HttpHeaderInspectingAuthenticatorConfigurationElement config);
  88. }
  89.  
  90. public interface IHttpHeaderInspectingAuthenticatorFactory<T> : IHttpHeaderInspectingAuthenticatorFactory
  91.     where T: HttpHeaderInspectingAuthenticatorConfigurationElement
  92. {
  93.     IHttpHeaderInspectingAuthenticator<T> Construct(T config);
  94. }
  95.  
  96. public abstract class HttpHeaderInspectingAuthenticatorFactoryBase<T> : IHttpHeaderInspectingAuthenticatorFactory<T>
  97.             where T : HttpHeaderInspectingAuthenticatorConfigurationElement
  98. {
  99.     protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  100.  
  101.     public abstract IHttpHeaderInspectingAuthenticator<T> Construct(T config);
  102.  
  103.     public IHttpHeaderInspectingAuthenticator Construct(HttpHeaderInspectingAuthenticatorConfigurationElement config)
  104.     {
  105.         return Construct((T)config);
  106.     }
  107. }
  108.  
  109.  public class BasicAuthenticationInspectingAuthenticatorFactory :
  110.     HttpHeaderInspectingAuthenticatorFactoryBase<BasicAuthenticationHeaderInspectorConfigurationElement>
  111. {
  112.     public override IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement> Construct(BasicAuthenticationHeaderInspectorConfigurationElement config)
  113.     {
  114.         return new BasicAuthenticationInspectingAuthenticator(config);
  115.     }
  116. }
  117.        
  118. public IHttpHeaderInspectingAuthenticator GetInspector()
  119. {
  120.     var factoryInstance = (IHttpHeaderInspectingAuthenticatorFactory)Activator.CreateInstance(Type.GetType(Factory));
  121.     return factoryInstance.Construct(this);
  122. }
  123.        
  124. dynamic factoryInstance = Activator.CreateInstance(Type.GetType(Factory));
  125.  dynamic parameter = this;
  126.  return factoryInstance.Construct(parameter);