Пример #1
0
        protected XrmServiceContext CreateXrmServiceContext(MergeOption?mergeOption = null)
        {
            string organizationUri = ConfigurationManager.AppSettings["organizationUri"];

            //IServiceManagement<IOrganizationService> OrganizationServiceManagement = null;
            //AuthenticationProviderType OrgAuthType;
            //AuthenticationCredentials authCredentials = null;
            //AuthenticationCredentials tokenCredentials = null;
            //OrganizationServiceProxy organizationProxy = null;
            //SecurityTokenResponse responseToken = null;

            //try
            //{
            //    OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));
            //    OrgAuthType = OrganizationServiceManagement.AuthenticationType;
            //    authCredentials = GetCredentials(OrgAuthType);
            //    tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials);
            //    organizationProxy = null;
            //    responseToken = tokenCredentials.SecurityTokenResponse;
            //}
            //catch
            //{

            //}

            IServiceManagement <IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement <IOrganizationService>(new Uri(organizationUri));
            AuthenticationProviderType OrgAuthType       = OrganizationServiceManagement.AuthenticationType;
            AuthenticationCredentials  authCredentials   = GetCredentials(OrgAuthType);
            AuthenticationCredentials  tokenCredentials  = OrganizationServiceManagement.Authenticate(authCredentials);
            OrganizationServiceProxy   organizationProxy = null;
            SecurityTokenResponse      responseToken     = tokenCredentials.SecurityTokenResponse;

            if (ConfigurationManager.AppSettings["CRM_AuthenticationType"] == "ActiveDirectory")
            {
                using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, authCredentials.ClientCredentials))
                {
                    organizationProxy.EnableProxyTypes();
                }
            }
            else
            {
                using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken))
                {
                    organizationProxy.EnableProxyTypes();
                }
            }

            IOrganizationService service = (IOrganizationService)organizationProxy;

            var context = new XrmServiceContext(service);

            if (context != null && mergeOption != null)
            {
                context.MergeOption = mergeOption.Value;
            }
            return(context);
        }
Пример #2
0
        protected bool CheckIfTokenIsExpired(SecurityTokenResponse token, TimeSpan?expiryWindow)
        {
            var now      = DateTime.UtcNow;
            var duration = token.Token.ValidTo - token.Token.ValidFrom;

            if (expiryWindow == null || duration < expiryWindow.Value)
            {
                return(now >= token.Token.ValidTo);
            }

            var expired = (now + expiryWindow.Value) > token.Token.ValidTo;

            return(expired);
        }
Пример #3
0
        /// <summary>
        /// Creates a Discovery Service Proxy object.
        /// </summary>
        /// <param name="crmSdkServerUrl">The discovery service base URL.</param>
        /// <param name="isIfdDeployment">Boolean indicating if this is an IFD.</param>
        /// <param name="ifdUserName">Admin UserName for IFD</param>
        /// <param name="ifdPassword">Admin PWD for IFD</param>
        /// <returns></returns>
        public static DiscoveryServiceProxy CreateDiscoveryProxy(string crmSdkServerUrl, bool isIfdDeployment = false, string ifdUserName = "", string ifdPassword = "")
        {
            Uri orgUri = GetDiscoveryServiceUri(crmSdkServerUrl);
            IServiceConfiguration <IOrganizationService> orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration <IOrganizationService>(orgUri);

            var credentials = new ClientCredentials();

            if (isIfdDeployment)
            {
                credentials.UserName.UserName = ifdUserName;
                credentials.UserName.Password = ifdPassword;
            }

            IServiceConfiguration <IDiscoveryService> discoveryConfiguration =
                ServiceConfigurationFactory.CreateConfiguration <IDiscoveryService>(orgUri);
            SecurityTokenResponse userResponseWrapper = discoveryConfiguration.Authenticate(credentials);

            return(new DiscoveryServiceProxy(discoveryConfiguration, userResponseWrapper));
        }
        private static void ReNewAdminSecurityTokenIfNeeds()
        {
            lock (MSCRMConnection._locker)
            {
                if (MSCRMConnection._adminSecurityToken == null || DateTime.UtcNow.AddMinutes(15) >= _adminSecurityToken.Response.Lifetime.Expires)
                {
                    if (adminService != null)
                    {
                        adminService.Dispose();
                    }


                    AuthenticationCredentials authCredentials = new AuthenticationCredentials();
                    authCredentials.ClientCredentials.UserName.UserName        = MSCRMConnection.UserName;
                    authCredentials.ClientCredentials.UserName.Password        = MSCRMConnection.Password;
                    authCredentials.ClientCredentials.UseIdentityConfiguration = true;

                    MSCRMConnection._adminSecurityToken = MSCRMConnection._orgServiceManagement.Authenticate(authCredentials).SecurityTokenResponse;
                }
            }
        }
        public CrmConnector(string userName, string password, string organizationUrl)
        {
            var credentials = new ClientCredentials();

            credentials.UserName.UserName = userName;
            credentials.UserName.Password = password;

            var authCredentials = new AuthenticationCredentials
            {
                ClientCredentials     = credentials,
                SupportingCredentials = new AuthenticationCredentials
                {
                    ClientCredentials = DeviceIdManager.LoadOrRegisterDevice()
                }
            };

            orgServiceManagement =
                ServiceConfigurationFactory.CreateManagement <IOrganizationService>(new Uri(organizationUrl));
            var tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

            organizationTokenResponse = tokenCredentials.SecurityTokenResponse;
        }
 /// <summary>
 /// Constructor with an authenticated security token that will renew using on the supplied credentials
 /// </summary>
 /// <param name="serviceManagement">The service configuration</param>
 /// <param name="securityTokenResponse">The existing security token response from prior authentication request</param>
 /// <param name="credentials">The credentails of the identity being used for authentication</param>
 /// <remarks>
 /// The credentials are required for proxy to perform subsequent authentice requests to obtain new valid tokens
 /// </remarks>
 public ManagedTokenDiscoveryServiceProxy(IServiceManagement <IDiscoveryService> serviceManagement,
                                          SecurityTokenResponse securityTokenResponse, ClientCredentials credentials)
     : this(serviceManagement, credentials)
 {
     this.SecurityTokenResponse = securityTokenResponse;
 }
 /// <summary>Initializes a new instance of the DiscoveryServiceProxy class using a service management and a security token response.</summary>
 /// <param name="serviceManagement">Type: <see cref="T:Microsoft.Xrm.Sdk.Client.IServiceConfiguration`1"></see>. A service management.</param>
 /// <param name="securityTokenResponse">Type: <see cref="P:Microsoft.Xrm.Sdk.Client.AuthenticationCredentials.SecurityTokenResponse"></see>. A security token response.</param>
 public AsyncDiscoveryServiceProxy(IServiceManagement <IWcfAsyncDiscoveryService> serviceManagement, SecurityTokenResponse securityTokenResponse)
     : this(serviceManagement as IServiceConfiguration <IWcfAsyncDiscoveryService>, securityTokenResponse)
 {
 }
 /// <summary>Initializes a new instance of the DiscoveryServiceProxy class using a service configuration and a security token response.</summary>
 /// <param name="serviceConfiguration">Type: <see cref="T:Microsoft.Xrm.Sdk.Client.IServiceConfiguration`1"></see>. A service configuration.</param>
 /// <param name="securityTokenResponse">Type: <see cref="P:Microsoft.Xrm.Sdk.Client.AuthenticationCredentials.SecurityTokenResponse"></see>. A security token response.</param>
 public AsyncDiscoveryServiceProxy(IServiceConfiguration <IWcfAsyncDiscoveryService> serviceConfiguration, SecurityTokenResponse securityTokenResponse)
     : base(serviceConfiguration, securityTokenResponse)
 {
 }
Пример #9
0
 public ManagedTokenOrganizationServiceProxy(IServiceManagement <IOrganizationService> serviceManagement,
                                             SecurityTokenResponse securityTokenRes)
     : base(serviceManagement, securityTokenRes)
 {
     this._proxyManager = new AutoRefreshSecurityToken <OrganizationServiceProxy, IOrganizationService>(this);
 }
Пример #10
0
 public ManagedTokenDiscoveryServiceProxy(IServiceManagement <IDiscoveryService> serviceManagement,
                                          SecurityTokenResponse securityTokenRes)
     : base(serviceManagement, securityTokenRes)
 {
     this._proxyManager = new AutoRefreshSecurityToken <DiscoveryServiceProxy, IDiscoveryService>(this);
 }
 /// <summary>Initializes a new instance of the <see cref="T:Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy"></see> class using a service configuration and a security token response.</summary>
 /// <param name="serviceConfiguration">Type: <see cref="T:Microsoft.Xrm.Sdk.Client.IServiceConfiguration`1"></see>&lt;<see cref="T:Microsoft.Xrm.Sdk.IOrganizationService"></see>&gt;. A service configuration.</param>
 /// <param name="securityTokenResponse">Type: <see cref="T:Microsoft.Xrm.Sdk.Client.SecurityTokenResponse"></see>. A security token response.</param>
 public AsyncOrganizationServiceProxy(IServiceConfiguration <IWcfAsyncOrganizationService> serviceConfiguration, SecurityTokenResponse securityTokenResponse)
     : base(serviceConfiguration, securityTokenResponse)
 {
 }
 public ManagedTokenDiscoveryServiceProxy(IServiceManagement<IDiscoveryService> serviceManagement,
     SecurityTokenResponse securityTokenRes)
     : base(serviceManagement, securityTokenRes)
 {
     this._proxyManager = new AutoRefreshSecurityToken<DiscoveryServiceProxy, IDiscoveryService>(this);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManagedTokenOrganizationServiceProxy"/> class.
 /// </summary>
 /// <param name="serviceManagement">The service management.</param>
 /// <param name="securityTokenResponse">The security token response.</param>
 public ManagedTokenOrganizationServiceProxy(SecurityTokenResponse securityTokenResponse, IServiceManagement <IOrganizationService> serviceManagement, ClientCredentials clientCredentials)
     : base(serviceManagement, clientCredentials)
 {
     base.SecurityTokenResponse = securityTokenResponse;
 }
Пример #14
0
 public void UpdateSecurityTokenResponse(SecurityTokenResponse securityTokenResponse)
 {
     this.SecurityTokenResponse = securityTokenResponse;
 }
        public SecurityTokenResponse Authenticate(ClientCredentials clientCredentials, SecurityTokenResponse deviceSecurityTokenResponse)
        {
#if SDK7AtLeast
            throw new InvalidOperationException("Authentication to MSA services is not supported.");
#else
            return(this.service.Authenticate(clientCredentials, deviceSecurityTokenResponse));
#endif
        }
Пример #16
0
 public ManagedTokenOrganizationServiceProxy(IServiceManagement <IOrganizationService> serviceManagement, SecurityTokenResponse securityTokenResponse)
     : base(serviceManagement, securityTokenResponse)
 {
 }
Пример #17
0
 public ManagedTokenDiscoveryServiceProxy(IServiceManagement <IDiscoveryService> serviceManagement, SecurityTokenResponse securityTokenResponse)
     : base(serviceManagement, securityTokenResponse)
 {
 }
 public SecurityTokenResponse Authenticate(ClientCredentials clientCredentials, SecurityTokenResponse deviceTokenResponse)
 {
     return(_authenticate4(_config, clientCredentials, deviceTokenResponse));
 }
 public SecurityTokenResponse Authenticate(ClientCredentials clientCredentials, SecurityTokenResponse deviceSecurityTokenResponse)
 {
     return(this.service.Authenticate(clientCredentials, deviceSecurityTokenResponse));
 }
 /// <summary>
 /// Constructor with an authenticated security token that will renew using on the supplied credentials
 /// </summary>
 /// <param name="serviceManagement">The service configuration</param>
 /// <param name="securityTokenResponse">The existing security token response from prior authentication request</param>
 /// <param name="credentials">The credentails of the identity being used for authentication</param>
 /// <remarks>
 /// The credentials are required for proxy to perform subsequent authentice requests to obtain new valid tokens
 /// </remarks>
 public ManagedTokenOrganizationServiceProxy(IServiceManagement <IOrganizationService> serviceManagement,
                                             SecurityTokenResponse securityTokenResponse, ClientCredentials credentials)
     : this(serviceManagement, credentials)
 {
     this.SecurityTokenResponse = securityTokenResponse;
 }