SecurityTokenAuthenticatorAdapter.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IdentityModel.Policy;
  7. using System.IdentityModel.Selectors;
  8. using System.IdentityModel.Tokens;
  9. using System.Security.Claims;
  10. namespace System.ServiceModel.Security
  11. {
  12. internal class SecurityTokenAuthenticatorAdapter : SecurityTokenAuthenticator
  13. {
  14. SecurityTokenHandler _securityTokenHandler;
  15. ExceptionMapper _exceptionMapper;
  16. public SecurityTokenAuthenticatorAdapter(SecurityTokenHandler securityTokenHandler, ExceptionMapper exceptionMapper)
  17. {
  18. if (securityTokenHandler == null)
  19. {
  20. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityTokenHandler");
  21. }
  22. if (exceptionMapper == null)
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exceptionMapper");
  25. }
  26. _securityTokenHandler = securityTokenHandler;
  27. _exceptionMapper = exceptionMapper;
  28. }
  29. protected override bool CanValidateTokenCore(SecurityToken token)
  30. {
  31. if (token == null)
  32. {
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  34. }
  35. return ((token.GetType() == _securityTokenHandler.TokenType) && (_securityTokenHandler.CanValidateToken));
  36. }
  37. protected sealed override ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore(SecurityToken token)
  38. {
  39. IEnumerable<ClaimsIdentity> subjectCollection = null;
  40. try
  41. {
  42. subjectCollection = _securityTokenHandler.ValidateToken(token);
  43. }
  44. catch (Exception ex)
  45. {
  46. if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex))
  47. {
  48. throw;
  49. }
  50. }
  51. List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
  52. policies.Add(new AuthorizationPolicy(subjectCollection));
  53. return policies.AsReadOnly();
  54. }
  55. }
  56. }