WindowsClientCredential.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System;
  7. using System.Net;
  8. using System.Security.Principal;
  9. using System.ServiceModel;
  10. public sealed class WindowsClientCredential
  11. {
  12. internal const TokenImpersonationLevel DefaultImpersonationLevel = TokenImpersonationLevel.Identification;
  13. TokenImpersonationLevel allowedImpersonationLevel = DefaultImpersonationLevel;
  14. NetworkCredential windowsCredentials;
  15. bool allowNtlm = SspiSecurityTokenProvider.DefaultAllowNtlm;
  16. bool isReadOnly;
  17. internal WindowsClientCredential()
  18. {
  19. }
  20. internal WindowsClientCredential(WindowsClientCredential other)
  21. {
  22. if (other.windowsCredentials != null)
  23. this.windowsCredentials = SecurityUtils.GetNetworkCredentialsCopy(other.windowsCredentials);
  24. this.allowedImpersonationLevel = other.allowedImpersonationLevel;
  25. this.allowNtlm = other.allowNtlm;
  26. this.isReadOnly = other.isReadOnly;
  27. }
  28. public TokenImpersonationLevel AllowedImpersonationLevel
  29. {
  30. get
  31. {
  32. return this.allowedImpersonationLevel;
  33. }
  34. set
  35. {
  36. ThrowIfImmutable();
  37. if (((value == TokenImpersonationLevel.None) || (value == TokenImpersonationLevel.Anonymous)) && System.ServiceModel.Channels.UnsafeNativeMethods.IsTailoredApplication.Value)
  38. {
  39. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.UnsupportedTokenImpersonationLevel, "AllowedImpersonationLevel", value.ToString())));
  40. }
  41. this.allowedImpersonationLevel = value;
  42. }
  43. }
  44. public NetworkCredential ClientCredential
  45. {
  46. get
  47. {
  48. if (this.windowsCredentials == null)
  49. this.windowsCredentials = new NetworkCredential();
  50. return this.windowsCredentials;
  51. }
  52. set
  53. {
  54. ThrowIfImmutable();
  55. this.windowsCredentials = value;
  56. }
  57. }
  58. [ObsoleteAttribute("This property is deprecated and is maintained for backward compatibility only. The local machine policy will be used to determine if NTLM should be used.")]
  59. public bool AllowNtlm
  60. {
  61. get
  62. {
  63. return this.allowNtlm;
  64. }
  65. set
  66. {
  67. ThrowIfImmutable();
  68. this.allowNtlm = value;
  69. }
  70. }
  71. internal void MakeReadOnly()
  72. {
  73. this.isReadOnly = true;
  74. }
  75. void ThrowIfImmutable()
  76. {
  77. if (this.isReadOnly)
  78. {
  79. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  80. }
  81. }
  82. }
  83. }