NtlmAuthenticator.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. namespace SharpCifs.Smb
  18. {
  19. /// <summary>
  20. /// This class can be extended by applications that wish to trap authentication related exceptions and automatically retry the exceptional operation with different credentials.
  21. /// </summary>
  22. /// <remarks>
  23. /// This class can be extended by applications that wish to trap authentication related exceptions and automatically retry the exceptional operation with different credentials. Read <a href="../../../authhandler.html">jCIFS Exceptions and NtlmAuthenticator</a> for complete details.
  24. /// </remarks>
  25. public abstract class NtlmAuthenticator
  26. {
  27. private static NtlmAuthenticator _auth;
  28. private string _url;
  29. private SmbAuthException _sae;
  30. private void Reset()
  31. {
  32. _url = null;
  33. _sae = null;
  34. }
  35. /// <summary>Set the default <tt>NtlmAuthenticator</tt>.</summary>
  36. /// <remarks>
  37. /// Set the default <tt>NtlmAuthenticator</tt>. Once the default authenticator is set it cannot be changed. Calling this metho again will have no effect.
  38. /// </remarks>
  39. public static void SetDefault(NtlmAuthenticator a)
  40. {
  41. lock (typeof(NtlmAuthenticator))
  42. {
  43. if (_auth != null)
  44. {
  45. return;
  46. }
  47. _auth = a;
  48. }
  49. }
  50. protected internal string GetRequestingUrl()
  51. {
  52. return _url;
  53. }
  54. protected internal SmbAuthException GetRequestingException()
  55. {
  56. return _sae;
  57. }
  58. /// <summary>
  59. /// Used internally by jCIFS when an <tt>SmbAuthException</tt> is trapped to retrieve new user credentials.
  60. /// </summary>
  61. /// <remarks>
  62. /// Used internally by jCIFS when an <tt>SmbAuthException</tt> is trapped to retrieve new user credentials.
  63. /// </remarks>
  64. public static NtlmPasswordAuthentication RequestNtlmPasswordAuthentication(string url,
  65. SmbAuthException sae)
  66. {
  67. if (_auth == null)
  68. {
  69. return null;
  70. }
  71. lock (_auth)
  72. {
  73. _auth._url = url;
  74. _auth._sae = sae;
  75. return _auth.GetNtlmPasswordAuthentication();
  76. }
  77. }
  78. /// <summary>An application extending this class must provide an implementation for this method that returns new user credentials try try when accessing SMB resources described by the <tt>getRequestingURL</tt> and <tt>getRequestingException</tt> methods.
  79. /// </summary>
  80. /// <remarks>
  81. /// An application extending this class must provide an implementation for this method that returns new user credentials try try when accessing SMB resources described by the <tt>getRequestingURL</tt> and <tt>getRequestingException</tt> methods.
  82. /// If this method returns <tt>null</tt> the <tt>SmbAuthException</tt> that triggered the authenticator check will simply be rethrown. The default implementation returns <tt>null</tt>.
  83. /// </remarks>
  84. protected internal virtual NtlmPasswordAuthentication GetNtlmPasswordAuthentication()
  85. {
  86. return null;
  87. }
  88. }
  89. }