Greetings,
We are developing application which is interacting with different versions and deployments of MS Dynamics CRM.
For one of these CRMs we get this error message:
System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---> System.ServiceModel.FaultException: ID3242: The security
token could not be authenticated or authorized.
Details for this CRM: Dynamics CRM 2015, On-Premise deployment with ADFS and IFD. The CRM and our app are on different servers.
It is happening when our application has established connection and the same connection is used more than 60 minutes. I found that the default lifetime for security token is again 60 minutes.
For every interaction with the CRM after this period we are getting the above error.
More info for the Dynamics SDK in our app:
For authentication we are using some of the helper classes in Microsoft Dynamics CRM 2013 SDK - SDK\SampleCode\CS\HelperCode\
Also Microsoft Dynamics CRM 2013 SDK core assemblies -version 6.1.1
What I can see from the code is that if the token is going to expire the next 15 or less minutes, it will be refreshed (re-authenticate):
public void RenewTokenIfRequired() { if (null != this._proxy.SecurityTokenResponse && DateTime.UtcNow.AddMinutes(15) >= this._proxy.SecurityTokenResponse.Response.Lifetime.Expires) { try { this._proxy.Authenticate(); } catch (CommunicationException) { if (null == this._proxy.SecurityTokenResponse || DateTime.UtcNow >= this._proxy.SecurityTokenResponse.Response.Lifetime.Expires) { throw; } // Ignore the exception } } }
We are using the same code from CrmServiceHelpers.cs with no modifications.
I tried to force the token refresh by setting AddMinutes(15) to 60 minutes, but the expiration time has not changed.
I can provide a piece of the source code which is used by us to establish connection with Dynamics CRM.
I really will appreciate some advices how at least to force security token refresh.
Please let me know if you need more specific information.
Regards,
Martin