PnP-Sites-Core icon indicating copy to clipboard operation
PnP-Sites-Core copied to clipboard

Compile error when trying to catch SecurityTokenExpiredException exception

Open jackpoz opened this issue 5 years ago • 3 comments

Category

[x] Bug [ ] Enhancement

Environment

[x] Office 365 / SharePoint Online [ ] SharePoint 2016 [ ] SharePoint 2013

Using SharePointPnPCoreOnline from Nuget, version 3.11.1907

Expected or Desired Behavior

It should be possible to catch exception of type SecurityTokenExpiredException.

Observed Behavior

Trying to catch exception of type SecurityTokenExpiredException defined in namespace Microsoft.IdentityModel.Tokens causes a compiler error

Error CS0433 The type 'SecurityTokenExpiredException' exists in both 'Microsoft.IdentityModel.Tokens, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

Steps to Reproduce

  • Create a new Console Project
  • Add Nuget package SharePointPnPCoreOnline
  • Add Microsoft.IdentityModel.Tokens namespace in the using directives
  • Try to catch SecurityTokenExpiredException exception
  • Build
  • Notice the build error

Here's a code snippet to reproduce the issue

using Microsoft.IdentityModel.Tokens;

namespace ReproProject
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
            }
            catch(SecurityTokenExpiredException)
            {
            }
        }
    }
}

Here's a project to reproduce the issue ReproProject.zip

This is caused by the dependencies defined in https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/packages.config with both Microsoft.IdentityModel and Microsoft.IdentityModel.Tokens being included.

jackpoz avatar Jul 25 '19 16:07 jackpoz

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

ghost avatar Jul 25 '19 16:07 ghost

As a workaround I ended up catching the base class of SecurityTokenExpiredException from both dlls

try
{
}
catch(Exception ex) when (ex is Microsoft.IdentityModel.Tokens.SecurityTokenValidationException || ex is System.IdentityModel.Tokens.SecurityTokenExpiredException)
{
}

jackpoz avatar Jul 25 '19 16:07 jackpoz

Same error creating a SecurityTokenDescriptor

new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor {...}

vallemar avatar Sep 28 '20 13:09 vallemar