NtlmAuthenticator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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>This class can be extended by applications that wish to trap authentication related exceptions and automatically retry the exceptional operation with different credentials.
  20. /// </summary>
  21. /// <remarks>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.
  22. /// </remarks>
  23. public abstract class NtlmAuthenticator
  24. {
  25. private static NtlmAuthenticator _auth;
  26. private string _url;
  27. private SmbAuthException _sae;
  28. private void Reset()
  29. {
  30. _url = null;
  31. _sae = null;
  32. }
  33. /// <summary>Set the default <tt>NtlmAuthenticator</tt>.</summary>
  34. /// <remarks>Set the default <tt>NtlmAuthenticator</tt>. Once the default authenticator is set it cannot be changed. Calling this metho again will have no effect.
  35. /// </remarks>
  36. public static void SetDefault(NtlmAuthenticator a)
  37. {
  38. lock (typeof(NtlmAuthenticator))
  39. {
  40. if (_auth != null)
  41. {
  42. return;
  43. }
  44. _auth = a;
  45. }
  46. }
  47. protected internal string GetRequestingUrl()
  48. {
  49. return _url;
  50. }
  51. protected internal SmbAuthException GetRequestingException()
  52. {
  53. return _sae;
  54. }
  55. /// <summary>Used internally by jCIFS when an <tt>SmbAuthException</tt> is trapped to retrieve new user credentials.
  56. /// </summary>
  57. /// <remarks>Used internally by jCIFS when an <tt>SmbAuthException</tt> is trapped to retrieve new user credentials.
  58. /// </remarks>
  59. public static NtlmPasswordAuthentication RequestNtlmPasswordAuthentication(string
  60. url, SmbAuthException sae)
  61. {
  62. if (_auth == null)
  63. {
  64. return null;
  65. }
  66. lock (_auth)
  67. {
  68. _auth._url = url;
  69. _auth._sae = sae;
  70. return _auth.GetNtlmPasswordAuthentication();
  71. }
  72. }
  73. /// <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.
  74. /// </summary>
  75. /// <remarks>
  76. /// 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.
  77. /// 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>.
  78. /// </remarks>
  79. protected internal virtual NtlmPasswordAuthentication GetNtlmPasswordAuthentication
  80. ()
  81. {
  82. return null;
  83. }
  84. }
  85. }