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?

1
  • 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.? Sep 13, 2018 at 8:24

2 Answers 2

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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