PnP-Sites-Core
PnP-Sites-Core copied to clipboard
Compile error when trying to catch SecurityTokenExpiredException exception
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.
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
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)
{
}
Same error creating a SecurityTokenDescriptor
new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor {...}