CrmDiscoveryService是MS Crm中一个战略层次的服务, 它允许调用者确定在Crm中隶属的组织以及用来访问它们的Url, 我们可以用一个请求(Request)来让 CrmDiscovery Service 返回一个调用者隶属组织的列表.
以下是Active Directory (On-Premise)方式的认证
以下是ActiveDirectory (SPLA)的认证模式
以下是Active Directory (On-Premise)方式的认证
- using System;
- using System.Collections.Generic;
- using System.Text;
- //using CrmSdk.Discovery namespace.
- //using CrmSdk;
- using System.Net;
- using VAR.utility;
- namespace discoverysample
- {
- using CrmSdk.Discovery;
- class Program
- {
- static void Main(string[] args)
- {
- //Create a instance of CrmDiscoveryService
- try
- {
- utility.log(@"please configure app.config before run the application.");
- utility.log("Create CrmDiscoveryService start");
- discoverysample.CrmSdk.Discovery.CrmDiscoveryService discovery = new discoverysample.CrmSdk.Discovery.CrmDiscoveryService();
- //default we use the default credential
- utility.log("Create CrmDiscoveryService end");
- //discovery.UseDefaultCredentials = true;
- //discovery.Credentials = System.Net.CredentialCache.DefaultCredentials;
- //set the url we'll access.
- utility.log("get local configure");
- string hostname = System.Configuration.ConfigurationSettings.AppSettings["hostname"];
- utility.log("hostname:"+hostname );
- string port = System.Configuration.ConfigurationSettings.AppSettings["port"];
- utility.log("port:" + port);
- string authtype = System.Configuration.ConfigurationSettings.AppSettings["authenticationtype"];
- utility.log("authtype:" + authtype);
- discovery.Url = String.Format("http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx", hostname, port, authtype);
- utility.log("CrmDiscoveryServiceUrl:" + discovery.Url);
- Console.Write("username:");
- string username = Console.ReadLine();
- Console.Write("password:");
- string password = utility.ReadPassword();
- utility.log("");
- discovery.Credentials = new System.Net.NetworkCredential(username, password);
- discovery.PreAuthenticate = true;
- //discovery.Credentials = new NetworkCredential(username, password);
- utility.log("Request OrganizationDetails information ");
- // Retrieve the organization name and endpoint Url from the CrmDiscoveryService Web service.
- RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
- RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discovery.Execute(orgRequest);
- utility.log("retrieve OrganizationDetails information");
- OrganizationDetail orgInfo = null;
- // Iterate through all the retrieved organizations
- foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
- {
- System.Reflection.PropertyInfo [] props= orgDetail.GetType().GetProperties();
- foreach (System.Reflection.PropertyInfo propInfo in props)
- {
- utility.log(" "+propInfo.Name +":"+propInfo.GetValue (orgDetail ,null));
- }
- }
- utility.log("sample complete!");
- }
- catch (System.Web.Services.Protocols.SoapException ex)
- {
- utility.log(ex.Detail.InnerText);
- }
- catch (Exception ex)
- {
- utility.log(ex.Message);
- }
- Console.Read();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace VAR.MSCrm.CrmTraining
- {
- using VAR.MSCrm.Service.CrmReference.CrmSdk ;
- using VAR.MSCrm.Service.CrmReference.CrmSdk.Discovery ;
- using VAR.MSCrm.Utility.CrmServiceWrapper ;
- class Program
- {
- static void Main(string[] args)
- {
- //Create a instance of CrmDiscoveryService
- try
- {
- utility.log(@"please configure app.config before run the application.");
- //default we use the default credential
- //discovery.UseDefaultCredentials = true;
- //set the url we'll access.
- utility.log("get local configure");
- string server = System.Configuration.ConfigurationSettings.AppSettings["server"];
- utility.log("server:{0}",server );
- string domain = System.Configuration.ConfigurationSettings.AppSettings["domain"];
- utility.log("domain:{0}",domain );
- string organizationName = System.Configuration.ConfigurationSettings.AppSettings["organizationName"];
- utility.log("organizationName:{0}",organizationName );
- string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];
- utility.log("userName:{0}",userName );
- string authenticationtype = System.Configuration.ConfigurationSettings.AppSettings["authenticationtype"];
- utility.log("authtype:{0}" , authenticationtype);
- utility.log("create discovery service");
- CrmDicoveryServiceWrapper discovery = new CrmDicoveryServiceWrapper(server, authenticationtype);
- utility.log("discovery serviced create completed");
- string password = utility.CapturePassword("please input your password:");
- //discovery.Credentials = new System.Net.NetworkCredential (username ,password,domain );
- //discovery.PreAuthenticate = true;
- //discovery.Credentials = new NetworkCredential(username, password);
- utility.log("retrevie organizations");
- // Retrieve the organization name and endpoint Url from the CrmDiscoveryService Web service.
- RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
- // Substitute an appropriate domain, username, and password here.
- orgRequest.UserId = domain + "//" + userName;
- orgRequest.Password = password;
- RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discovery.Execute(orgRequest);
- utility.log("list organizations:");
- OrganizationDetail orgInfo = null;
- // Iterate through all the retrieved organizations
- foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
- {
- System.Reflection.PropertyInfo [] props= orgDetail.GetType().GetProperties();
- foreach (System.Reflection.PropertyInfo propInfo in props)
- {
- utility.log(" "+propInfo.Name +":"+propInfo.GetValue (orgDetail ,null));
- }
- }
- utility.log("sample complete!");
- }
- catch (System.Web.Services.Protocols.SoapException ex)
- {
- utility.log(ex.Detail.InnerText);
- }
- catch (Exception ex)
- {
- utility.log(ex.Message);
- }
- Console.Read();
- }
- }
- }