DbConnectionPoolAuthenticationContextKey.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbConnectionPoolAuthenticationContextKey.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">harsudan</owner>
  6. // <owner current="true" primary="false">yuronhe</owner>
  7. //------------------------------------------------------------------------------
  8. using System;
  9. using System.Diagnostics;
  10. namespace System.Data.ProviderBase
  11. {
  12. /// <summary>
  13. /// Represents the key of dbConnectionPoolAuthenticationContext.
  14. /// All data members should be immutable and so, hashCode is pre-computed.
  15. /// </summary>
  16. sealed internal class DbConnectionPoolAuthenticationContextKey
  17. {
  18. /// <summary>
  19. /// Security Token Service Authority.
  20. /// </summary>
  21. private readonly string _stsAuthority;
  22. /// <summary>
  23. /// Service Principal Name.
  24. /// </summary>
  25. private readonly string _servicePrincipalName;
  26. /// <summary>
  27. /// Pre-Computed Hash Code.
  28. /// </summary>
  29. private readonly int _hashCode;
  30. internal string StsAuthority {
  31. get {
  32. return _stsAuthority;
  33. }
  34. }
  35. internal string ServicePrincipalName {
  36. get {
  37. return _servicePrincipalName;
  38. }
  39. }
  40. /// <summary>
  41. /// Constructor for the type.
  42. /// </summary>
  43. /// <param name="stsAuthority">Token Endpoint URL</param>
  44. /// <param name="servicePrincipalName">SPN representing the SQL service in an active directory.</param>
  45. internal DbConnectionPoolAuthenticationContextKey(string stsAuthority, string servicePrincipalName) {
  46. Debug.Assert(!string.IsNullOrWhiteSpace(stsAuthority));
  47. Debug.Assert(!string.IsNullOrWhiteSpace(servicePrincipalName));
  48. _stsAuthority = stsAuthority;
  49. _servicePrincipalName = servicePrincipalName;
  50. // Pre-compute hash since data members are not going to change.
  51. _hashCode = ComputeHashCode();
  52. }
  53. /// <summary>
  54. /// Override the default Equals implementation.
  55. /// </summary>
  56. /// <param name="obj"></param>
  57. /// <returns></returns>
  58. public override bool Equals(object obj) {
  59. if (obj == null) {
  60. return false;
  61. }
  62. DbConnectionPoolAuthenticationContextKey otherKey = obj as DbConnectionPoolAuthenticationContextKey;
  63. if (otherKey == null) {
  64. return false;
  65. }
  66. return (String.Equals(StsAuthority, otherKey.StsAuthority, StringComparison.InvariantCultureIgnoreCase)
  67. && String.Equals(ServicePrincipalName, otherKey.ServicePrincipalName, StringComparison.InvariantCultureIgnoreCase));
  68. }
  69. /// <summary>
  70. /// Override the default GetHashCode implementation.
  71. /// </summary>
  72. /// <returns></returns>
  73. public override int GetHashCode() {
  74. return _hashCode;
  75. }
  76. /// <summary>
  77. /// Compute the hash code for this object.
  78. /// </summary>
  79. /// <returns></returns>
  80. private int ComputeHashCode() {
  81. int hashCode = 33;
  82. unchecked
  83. {
  84. hashCode = (hashCode * 17) + StsAuthority.GetHashCode();
  85. hashCode = (hashCode * 17) + ServicePrincipalName.GetHashCode();
  86. }
  87. return hashCode;
  88. }
  89. }
  90. }