How To use GetAppOnlyAuthenticatedContext when accessing Sharepoint Online Site?

2018-10-23 23:32:51

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?

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;

}

The fully qualified catch recommended by Derek didn't work for me. I believe the name

  • 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;

    }

    2018-10-24 00:42:13
  • 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)

    2018-10-24 00:44:20