AuthenticationSchemesHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.ComponentModel;
  7. using System.Net;
  8. using System.Runtime;
  9. using System.Text;
  10. static class AuthenticationSchemesHelper
  11. {
  12. public static bool DoesAuthTypeMatch(AuthenticationSchemes authScheme, string authType)
  13. {
  14. if ((authType == null) || (authType.Length == 0))
  15. {
  16. return authScheme.IsSet(AuthenticationSchemes.Anonymous);
  17. }
  18. if (authType.Equals("kerberos", StringComparison.OrdinalIgnoreCase) ||
  19. authType.Equals("negotiate", StringComparison.OrdinalIgnoreCase))
  20. {
  21. return authScheme.IsSet(AuthenticationSchemes.Negotiate);
  22. }
  23. else if (authType.Equals("ntlm", StringComparison.OrdinalIgnoreCase))
  24. {
  25. return authScheme.IsSet(AuthenticationSchemes.Negotiate) ||
  26. authScheme.IsSet(AuthenticationSchemes.Ntlm);
  27. }
  28. AuthenticationSchemes authTypeScheme;
  29. if (!Enum.TryParse<AuthenticationSchemes>(authType, true, out authTypeScheme))
  30. {
  31. return false;
  32. }
  33. return authScheme.IsSet(authTypeScheme);
  34. }
  35. public static bool IsSingleton(this AuthenticationSchemes v)
  36. {
  37. bool result;
  38. switch (v)
  39. {
  40. case AuthenticationSchemes.Digest:
  41. case AuthenticationSchemes.Negotiate:
  42. case AuthenticationSchemes.Ntlm:
  43. case AuthenticationSchemes.Basic:
  44. case AuthenticationSchemes.Anonymous:
  45. result = true;
  46. break;
  47. default:
  48. result = false;
  49. break;
  50. }
  51. return result;
  52. }
  53. public static bool IsSet(this AuthenticationSchemes thisPtr, AuthenticationSchemes authenticationSchemes)
  54. {
  55. return (thisPtr & authenticationSchemes) == authenticationSchemes;
  56. }
  57. public static bool IsNotSet(this AuthenticationSchemes thisPtr, AuthenticationSchemes authenticationSchemes)
  58. {
  59. return (thisPtr & authenticationSchemes) == 0;
  60. }
  61. internal static string ToString(AuthenticationSchemes authScheme)
  62. {
  63. return authScheme.ToString().ToLowerInvariant();
  64. }
  65. }
  66. }