1

I want to access an SPO Site without using my username and password but with ClientId And Client Secret, I followed this answer on StackExchange, see reference but when I installed the NuGet I got an error "The type SecurityTokenExpiredException" exists in both Microsoft.Identity.Model.token?

https://stackoverflow.com/questions/47616155/access-sharepoint-list-using-client-id-and-client-secret-in-asp-net-c-sharp

Anybody knows how to resolve the issue?

  • Have you registered an app and granted permissions for the app on the SharePoint site..?? When are you getting this error..?? On adding the nuget or while generating the client context.? – Suresh Kumar Senapati Sep 13 '18 at 8:24
1

I started getting this error recently as well. The code with this error comes from the SharePointContext.cs file from the App for SharePoint Nuget package.

You can handle this in one of two ways: 1. Fully qualify the type.

 catch (Microsoft.IdentityModel.Tokens.SecurityTokenExpiredException)
 {
   contextTokenExpired = true;
 }

or 2. Just catch a generic Exception:

 catch (Exception)
 {
    contextTokenExpired = true;
 }
1

The fully qualified catch recommended by Derek didn't work for me. I believe the namespace for the exception is the same 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'. I got around it by--instead of backing WAY off and using a base Exception class like Derek suggested--using the class the exception at Microsoft.IdentityModel.Tokens.SecurityTokenExpiredException inherits from: Microsoft.IdentityModel.Tokens.SecurityTokenValidationException. Thus, my solution changed the catch block to:

catch (Microsoft.IdentityModel.Tokens.SecurityTokenValidationException)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.