Ejemplo n.º 1
0
        //CrmServiceClient를 사용하여 Microsoft Dynamics 365(온라인 및 온-프레미스) 웹 서비스에 연결한다.
        /// <summary>
        /// Deprecated (Ws-Trust)
        /// connect to the Organization service.
        /// Connect to the Microsoft Dynamics 365 (online & on-premises) web service using the CrmServiceClient
        /// </summary>
        /// <see cref="https://msdn.microsoft.com/en-us/library/jj602970.aspx"/>
        /// <seealso cref="https://rajeevpentyala.com/2016/12/11/code-snippet-connect-to-dynamics-crm-using-organization-service-c/"/>
        /// <param name="connectionString">Provides service connection information</param>
        /// <param name="orgName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="location"></param>
        public Guid ConnectService(string orgName, string userName, string password, Definition.Enum.Location location)
        {
            var uri = new System.Uri($"https://{orgName}.api.crm{location.GetStringValue()}.dynamics.com/XRMServices/2011/Organization.svc");

            //기본인증정보 설정.
            if (!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(password))
            {
                if (ClientCredentials == null)
                {
                    ClientCredentials = new ClientCredentials();
                }
                ClientCredentials.UserName.UserName = userName;
                ClientCredentials.UserName.Password = password;
            }

            if (_deviceCredentials == null)
            {
                _deviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
            }

            var organizationServiceProxy = new OrganizationServiceProxy(uri, null, ClientCredentials, _deviceCredentials);

            Service = new CrmServiceClient(organizationServiceProxy);

            return(((WhoAmIResponse)Service.Execute(new WhoAmIRequest())).UserId);
        }
        public static List <string> GetOrganizations(string url, string domain, string username, string password)
        {
            var results = new List <string>()
            {
            };
            var credentials = GetCredentials(url, domain, username, password);
            ClientCredentials deviceCredentials = null;

            if (url.IndexOf("dynamics.com", StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                deviceCredentials = DeviceIdManager.LoadOrRegisterDevice(new Guid());   // TODO this was failing with some online connections
            }

            using (DiscoveryServiceProxy disco = new DiscoveryServiceProxy(new Uri(url), null, credentials, deviceCredentials))
            {
                if (disco != null)
                {
                    OrganizationDetailCollection orgs = DiscoverOrganizations(disco);
                    if (orgs.Count > 0)
                    {
                        results = orgs.Select(o => o.FriendlyName).ToList();
                    }
                }
            }

            return(results);
        }
        private void AuthenticateLiveIdCredentials(ClientCredentials clientCredentials)
        {
            var deviceCredentials = this.ServiceManagement.IssuerEndpoints.ContainsKey("Username")
                ? DeviceIdManager.LoadOrRegisterDevice(this.ServiceManagement.IssuerEndpoints["Username"].IssuerAddress.Uri)
                : DeviceIdManager.LoadOrRegisterDevice();

            AuthenticateLiveIdCredentials(clientCredentials, deviceCredentials);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Prepare and authenticate client credentials and supporting device credentials for LiveID scenario
        /// </summary>
        /// <param name="clientCredentials">The client credentials (Microsoft Account)</param>
        /// <remarks>Implicitly registers device credentials using deviceidmanager.cs helper</remarks>
        private void AuthenticateLiveIdCredentials(ClientCredentials clientCredentials)
        {
            //Attempt to call .LoadOrRegisterDevice using IssuerEndpoint to load existing and/or persist to file.
            var deviceCredentials = this.ServiceManagement.IssuerEndpoints.ContainsKey("Username")
                ? DeviceIdManager.LoadOrRegisterDevice(this.ServiceManagement.IssuerEndpoints["Username"].IssuerAddress.Uri)
                : DeviceIdManager.LoadOrRegisterDevice();

            AuthenticateLiveIdCredentials(clientCredentials, deviceCredentials);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Authenticates the device token
        /// </summary>
        /// <returns>Generated SecurityTokenResponse for the device</returns>
        public SecurityTokenResponse AuthenticateDevice()
        {
            if (null == this._deviceCredentials)
            {
                this._deviceCredentials = DeviceIdManager.LoadOrRegisterDevice(
                    this._proxy.ServiceConfiguration.CurrentIssuer.IssuerAddress.Uri);
            }

            return(this._proxy.ServiceConfiguration.AuthenticateDevice(this._deviceCredentials));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Obtain the AuthenticationCredentials based on AuthenticationProviderType.
        /// </summary>
        /// <param name="service">A service management object.</param>
        /// <param name="endpointType">An AuthenticationProviderType of the CRM environment.</param>
        /// <returns>Get filled credentials.</returns>
        private AuthenticationCredentials GetCredentials <TService>(IServiceManagement <TService> service, AuthenticationProviderType endpointType)
        {
            AuthenticationCredentials authCredentials = new AuthenticationCredentials();

            _userName = username.Text;
            _password = txtPassword.Text;

            switch (endpointType)
            {
            case AuthenticationProviderType.ActiveDirectory:
                authCredentials.ClientCredentials.Windows.ClientCredential =
                    new System.Net.NetworkCredential(_userName,
                                                     _password,
                                                     _domain);
                break;

            case AuthenticationProviderType.LiveId:
                authCredentials.ClientCredentials.UserName.UserName = _userName;
                authCredentials.ClientCredentials.UserName.Password = _password;
                authCredentials.SupportingCredentials = new AuthenticationCredentials();
                //authCredentials.SupportingCredentials.ClientCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
                break;

            default:     // For Federated and OnlineFederated environments.
                authCredentials.ClientCredentials.UserName.UserName = _userName;
                authCredentials.ClientCredentials.UserName.Password = _password;
                // For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
                // authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  // Windows Kerberos

                // The service is configured for User Id authentication, but the user might provide Microsoft
                // account credentials. If so, the supporting credentials must contain the device credentials.
                if (endpointType == AuthenticationProviderType.OnlineFederation)
                {
                    IdentityProvider provider = service.GetIdentityProvider(authCredentials.ClientCredentials.UserName.UserName);
                    if (provider != null && provider.IdentityProviderType == IdentityProviderType.LiveId)
                    {
                        authCredentials.SupportingCredentials = new AuthenticationCredentials();
                        //authCredentials.SupportingCredentials.ClientCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
                        authCredentials.SupportingCredentials.ClientCredentials = DeviceIdManager.LoadOrRegisterDevice();
                    }
                }

                break;
            }

            return(authCredentials);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Obtain the AuthenticationCredentials based on AuthenticationProviderType.
        /// </summary>
        /// <see href="https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/sample-authenticate-users-web-services"/>
        /// <typeparam name="TService"></typeparam>
        /// <param name="service">A service management object.</param>
        /// <param name="endpointType">An <code>AuthenticationProviderType</code> of the CRM environment.</param>
        /// <param name="userid"></param>
        /// <param name="userpw"></param>
        /// <param name="domain"></param>
        /// <returns>Get filled <code>AuthenticationCredentials</code>.</returns>
        public AuthenticationCredentials GetCredentials <TService>(IServiceManagement <TService> service,
                                                                   AuthenticationProviderType endpointType, string userid, string userpw, string domain)
        {
            AuthenticationCredentials authCredentials = new AuthenticationCredentials();

            switch (endpointType)
            {
            case AuthenticationProviderType.ActiveDirectory:
                authCredentials.ClientCredentials.Windows.ClientCredential = new NetworkCredential(userid, userpw, domain);
                break;

            case AuthenticationProviderType.LiveId:
                authCredentials.ClientCredentials.UserName.UserName = userid;
                authCredentials.ClientCredentials.UserName.Password = userpw;
                authCredentials.SupportingCredentials = new AuthenticationCredentials();
                authCredentials.SupportingCredentials.ClientCredentials = DeviceIdManager.LoadOrRegisterDevice();
                break;

            case AuthenticationProviderType.Federation:
            case AuthenticationProviderType.None:
                break;

            case AuthenticationProviderType.OnlineFederation:
                // For Federated and OnlineFederated environments.
                authCredentials.ClientCredentials.UserName.UserName = userid;
                authCredentials.ClientCredentials.UserName.Password = userpw;
                // For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
                // authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  // Windows Kerberos

                // The service is configured for User Id authentication, but the user might provide Microsoft
                // account credentials. If so, the supporting credentials must contain the device credentials.

                IdentityProvider provider = service.GetIdentityProvider(authCredentials.ClientCredentials.UserName.UserName);
                if (provider != null && provider.IdentityProviderType == IdentityProviderType.LiveId)
                {
                    authCredentials.SupportingCredentials = new AuthenticationCredentials();
                    authCredentials.SupportingCredentials.ClientCredentials =
                        DeviceIdManager.LoadOrRegisterDevice();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(endpointType), endpointType, null);
            }

            return(authCredentials);
        }
        public static IOrganizationService Connect(string url, string domain, string username, string password, string organization)
        {
            //var connectionString = @"Url=" + url + "; Username="******"; password="******";";
            //var connection = new Microsoft.Xrm.Client.CrmConnection(connectionString);
            //var test = new Microsoft.Xrm.Client.Services.OrganizationService(connection);
            //return test;

            var credentials = GetCredentials(url, domain, username, password);
            ClientCredentials deviceCredentials = null;

            if (url.IndexOf("dynamics.com", StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                deviceCredentials = DeviceIdManager.LoadOrRegisterDevice(new Guid());
            }

            Uri orgUri = null;
            OrganizationServiceProxy sdk = null;

            using (DiscoveryServiceProxy disco = new DiscoveryServiceProxy(new Uri(url), null, credentials, deviceCredentials))
            {
                if (disco != null)
                {
                    OrganizationDetailCollection orgs = DiscoverOrganizations(disco);
                    if (orgs.Count > 0)
                    {
                        var found = orgs.ToList()
                                    .Where(a => a.UniqueName.Equals(organization, StringComparison.InvariantCultureIgnoreCase))
                                    .Take(1).SingleOrDefault();

                        if (found != null)
                        {
                            orgUri = new Uri(found.Endpoints[EndpointType.OrganizationService]);
                        }
                    }
                }
            }

            if (orgUri != null)
            {
                sdk = new OrganizationServiceProxy(orgUri, null, credentials, deviceCredentials);
            }

            return(sdk);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// DepreCated (WS-Trust)
        /// Set clientCredential and Connect Server by Client using OrganizationServiceProxy to Custom URL
        /// </summary>
        /// <param name="organizationServiceUri"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Guid ConnectService(Uri organizationServiceUri, string userName, string password)
        {
            return(Guid.Empty);

            //기본인증정보 설정.
            if (!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(password))
            {
                if (ClientCredentials == null)
                {
                    ClientCredentials = new ClientCredentials();
                }
                ClientCredentials.UserName.UserName = userName;
                ClientCredentials.UserName.Password = password;
            }

            if (_deviceCredentials == null)
            {
                _deviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
            }

            OrganizationServiceProxy organizationServiceProxy = null;


            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
            try
            {
                organizationServiceProxy = new OrganizationServiceProxy(organizationServiceUri, null,
                                                                        ClientCredentials, _deviceCredentials);
            }
            catch
            {
                organizationServiceProxy = new OrganizationServiceProxy(organizationServiceUri, null,
                                                                        ClientCredentials, null);
            }

            organizationServiceProxy.Authenticate();

            Service = new CrmServiceClient(organizationServiceProxy);

            return(((WhoAmIResponse)Service.Execute(new WhoAmIRequest())).UserId);
        }
Ejemplo n.º 10
0
        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;
        }
Ejemplo n.º 11
0
 protected virtual ClientCredentials GetDeviceCredentials()
 {
     return(DeviceIdManager.LoadOrRegisterDevice());
 }
        public void LoadOrRegisterDeviceWithNameAndPasswordTest()
        {
            ClientCredentials clientCredentials = DeviceIdManager.LoadOrRegisterDevice("deviceName", "devicePasssword");

            Assert.IsNotNull(clientCredentials);
        }
        public void LoadOrRegisterDeviceTest()
        {
            ClientCredentials clientCredentials = DeviceIdManager.LoadOrRegisterDevice();

            Assert.IsNotNull(clientCredentials);
        }