SecurityContextProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using log4net.Util;
  21. namespace log4net.Core
  22. {
  23. /// <summary>
  24. /// The <see cref="SecurityContextProvider"/> providers default <see cref="SecurityContext"/> instances.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// A configured component that interacts with potentially protected system
  29. /// resources uses a <see cref="SecurityContext"/> to provide the elevated
  30. /// privileges required. If the <see cref="SecurityContext"/> object has
  31. /// been not been explicitly provided to the component then the component
  32. /// will request one from this <see cref="SecurityContextProvider"/>.
  33. /// </para>
  34. /// <para>
  35. /// By default the <see cref="SecurityContextProvider.DefaultProvider"/> is
  36. /// an instance of <see cref="SecurityContextProvider"/> which returns only
  37. /// <see cref="NullSecurityContext"/> objects. This is a reasonable default
  38. /// where the privileges required are not know by the system.
  39. /// </para>
  40. /// <para>
  41. /// This default behavior can be overridden by subclassing the <see cref="SecurityContextProvider"/>
  42. /// and overriding the <see cref="CreateSecurityContext"/> method to return
  43. /// the desired <see cref="SecurityContext"/> objects. The default provider
  44. /// can be replaced by programmatically setting the value of the
  45. /// <see cref="SecurityContextProvider.DefaultProvider"/> property.
  46. /// </para>
  47. /// <para>
  48. /// An alternative is to use the <c>log4net.Config.SecurityContextProviderAttribute</c>
  49. /// This attribute can be applied to an assembly in the same way as the
  50. /// <c>log4net.Config.XmlConfiguratorAttribute"</c>. The attribute takes
  51. /// the type to use as the <see cref="SecurityContextProvider"/> as an argument.
  52. /// </para>
  53. /// </remarks>
  54. /// <author>Nicko Cadell</author>
  55. public class SecurityContextProvider
  56. {
  57. /// <summary>
  58. /// The default provider
  59. /// </summary>
  60. private static SecurityContextProvider s_defaultProvider = new SecurityContextProvider();
  61. /// <summary>
  62. /// Gets or sets the default SecurityContextProvider
  63. /// </summary>
  64. /// <value>
  65. /// The default SecurityContextProvider
  66. /// </value>
  67. /// <remarks>
  68. /// <para>
  69. /// The default provider is used by configured components that
  70. /// require a <see cref="SecurityContext"/> and have not had one
  71. /// given to them.
  72. /// </para>
  73. /// <para>
  74. /// By default this is an instance of <see cref="SecurityContextProvider"/>
  75. /// that returns <see cref="NullSecurityContext"/> objects.
  76. /// </para>
  77. /// <para>
  78. /// The default provider can be set programmatically by setting
  79. /// the value of this property to a sub class of <see cref="SecurityContextProvider"/>
  80. /// that has the desired behavior.
  81. /// </para>
  82. /// </remarks>
  83. public static SecurityContextProvider DefaultProvider
  84. {
  85. get { return s_defaultProvider; }
  86. set { s_defaultProvider = value; }
  87. }
  88. /// <summary>
  89. /// Protected default constructor to allow subclassing
  90. /// </summary>
  91. /// <remarks>
  92. /// <para>
  93. /// Protected default constructor to allow subclassing
  94. /// </para>
  95. /// </remarks>
  96. protected SecurityContextProvider()
  97. {
  98. }
  99. /// <summary>
  100. /// Create a SecurityContext for a consumer
  101. /// </summary>
  102. /// <param name="consumer">The consumer requesting the SecurityContext</param>
  103. /// <returns>An impersonation context</returns>
  104. /// <remarks>
  105. /// <para>
  106. /// The default implementation is to return a <see cref="NullSecurityContext"/>.
  107. /// </para>
  108. /// <para>
  109. /// Subclasses should override this method to provide their own
  110. /// behavior.
  111. /// </para>
  112. /// </remarks>
  113. public virtual SecurityContext CreateSecurityContext(object consumer)
  114. {
  115. return NullSecurityContext.Instance;
  116. }
  117. }
  118. }