locked
CRM 2015 + Error : Security Token expired RRS feed

  • Question

  • Hi,

    I have created a .net application that connects to CRM and performs some CRUD operations.However if there are more number of records fetched from CRM and the transaction becomes long , the serviceProxy object expires.

    I searched on Internet and found the following solution. However i am not able to make out how should use it in my code.

    Below is my code to connect to CRM -

       String url = "https://xxxxx.api.crm4.dynamics.com/XRMServices/2011/Organization.svc""

       Uri organizationUri = new Uri(url);

      IServiceManagement<IOrganizationService> orgServiceManagement =
                      ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
                       organizationUri);

      AuthenticationProviderType endpointType = orgServiceManagement.AuthenticationType;


      AuthenticationCredentials authCredentials = new AuthenticationCredentials();
      authCredentials.UserPrincipalName = "xxxx@bayer.com";

      AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

      SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;

      using (serviceProxy = new OrganizationServiceProxy(orgServiceManagement, responseToken))
                    {
                        serviceProxy.EnableProxyTypes();
                    }

    Here is the code i found to renew the security token - 

            /// <summary>
            /// Wrapper class for OrganizationServiceProxy to support auto refresh security token
            /// </summary>
            internal sealed class ManagedTokenOrganizationServiceProxy : OrganizationServiceProxy
            {
                private AutoRefreshSecurityToken<OrganizationServiceProxy, IOrganizationService> _proxyManager;

                public ManagedTokenOrganizationServiceProxy(IServiceManagement<IOrganizationService> serviceManagement,
                    SecurityTokenResponse securityTokenRes)
                    : base(serviceManagement, securityTokenRes)
                {
                    this._proxyManager = new AutoRefreshSecurityToken<OrganizationServiceProxy, IOrganizationService>(this);
                }

                protected override void ValidateAuthentication()
                {
                    this._proxyManager.RenewTokenIfRequired();
                    base.ValidateAuthentication();
                }
            }

            /// <summary>
            /// Class that wraps acquiring the security token for a service
            /// </summary>
            public sealed class AutoRefreshSecurityToken<TProxy, TService>
                where TProxy : ServiceProxy<TService>
                where TService : class
            {
                private ClientCredentials _deviceCredentials;
                private TProxy _proxy;

                /// <summary>
                /// Instantiates an instance of the proxy class
                /// </summary>
                /// <param name="proxy">Proxy that will be used to authenticate the user</param>
                public AutoRefreshSecurityToken(TProxy proxy)
                {
                    if (null == proxy)
                    {
                        throw new ArgumentNullException("proxy");
                    }

                    this._proxy = proxy;
                }

                /// <summary>
                /// Renews the token (if it is near expiration or has expired)
                /// </summary>
                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;
                            }
                        }
                    }
                }
            }

    Basically i want to integrate the Security Token code  in .net App  code such that once the token expires it should renew the token rather than throwing error.

    Any help would be appreciated.

    Thanks,


    Tuesday, January 19, 2016 9:37 AM

All replies

  • Create an instance of ManagedTokenOrganizationServiceProxy instead OrganizationServiceProxy :

    Uri dInfo = new Uri(your config);
    ClientCredentials clientcred = new ClientCredentials();
    clientcred.UserName.UserName = YourUserName;
    clientcred.UserName.Password = YourPassword;

    //get the OrganizationService

    //OrganizationServiceProxy _serviceproxy = new OrganizationServiceProxy(new Uri(config.XrmOrgServiceProxy), null, clientcred, null);
     ManagedTokenOrganizationServiceProxy _serviceproxy = newManagedTokenOrganizationServiceProxy(newUri(YourURI), clientcred);





      
    Monday, February 13, 2017 12:42 AM