无法在访问Dynamic CRM Online Web Service

时间:2015-10-09 13:11:24

标签: microsoft-dynamics dynamics-crm-online

我需要利用公开的Dynamic CRM Data Service Endpoint来获取其中一种方法的数据。 服务(微软)帐户可以访问此服务。  我已尝试使用此处[https://msdn.microsoft.com/en-us/library/hh675404.aspx]提供的示例代码对Discovery Service和Organization Service进行身份验证并成功。但是我无法使用相同的身份验证来访问数据服务,因为我无论如何都可以找到将数据服务与其他两个相关联。使用网络凭据进行基本身份验证不起作用。

我已经下载了暴露的CSDL,并将其作为服务引用添加到我的项目中,该项目创建了一个从DataServiceContext扩展的Web服务类。我试图使用LinQ查询检索其中一个方法的数据。它引发了以下错误:

“响应有效负载不是有效的响应有效负载。请确保顶级元素是有效的Atom或JSON元素或属于'http://schemas.microsoft.com/ado/2007/08/dataservices'命名空间。”在使用小提琴捕获时,我意识到在点击数据服务URL时,它被重定向到登录页面'login.microsoftonline.com /'

有人可以建议一种方法来验证用户访问Data Serivce吗?

添加代码:

//<snippetAuthenticateWithNoHelp1>
            IServiceManagement<IDiscoveryService> serviceManagement =
                        ServiceConfigurationFactory.CreateManagement<IDiscoveryService>(
                        new Uri(_discoveryServiceAddress));
            AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;

            // Set the credentials.
            AuthenticationCredentials authCredentials = GetCredentials(serviceManagement, endpointType);


            String organizationUri = String.Empty;
            // Get the discovery service proxy.
            using (DiscoveryServiceProxy discoveryProxy =
                GetProxy<IDiscoveryService, DiscoveryServiceProxy>(serviceManagement, authCredentials))
            {
                // Obtain organization information from the Discovery service. 
                if (discoveryProxy != null)
                {
                    // Obtain information about the organizations that the system user belongs to.
                    OrganizationDetailCollection orgs = DiscoverOrganizations(discoveryProxy);
                    // Obtains the Web address (Uri) of the target organization.
                    organizationUri = FindOrganization(_organizationUniqueName,
                        orgs.ToArray()).Endpoints[EndpointType.OrganizationService];

                }
            }
            //</snippetAuthenticateWithNoHelp1>


            if (!String.IsNullOrWhiteSpace(organizationUri))
            {
                //<snippetAuthenticateWithNoHelp3>
                IServiceManagement<IOrganizationService> orgServiceManagement =
                    ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
                    new Uri(organizationUri));

                // Set the credentials.
                AuthenticationCredentials credentials = GetCredentials(orgServiceManagement, endpointType);

                // Get the organization service proxy.
                using (OrganizationServiceProxy organizationProxy =
                    GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceManagement, credentials))
                {
                    // This statement is required to enable early-bound type support.
                    organizationProxy.EnableProxyTypes();

                    // Now make an SDK call with the organization service proxy.
                    // Display information about the logged on user.
                    Guid userid = ((WhoAmIResponse)organizationProxy.Execute(
                        new WhoAmIRequest())).UserId;
                    SystemUser systemUser = organizationProxy.Retrieve("systemuser", userid,
                        new ColumnSet(new string[] { "firstname", "lastname" })).ToEntity<SystemUser>();
                    Console.WriteLine("Logged on user is {0} {1}.",
                        systemUser.FirstName, systemUser.LastName);
                    Uri x = new Uri("https://<MyOrgainzationName>.crm.dynamics.com/XRMServices/2011/OrganizationData.svc/");
                    MyOrgainzationContext saContext = new MyOrgainzationContext(x);
                    NetworkCredential nc = new NetworkCredential();
                    nc.UserName = "*****@microsoft.com";
                    nc.Password = "********";
                    saContext.Credentials = nc;
                    var query_where3 = from c in saContext.new_productSet
                                       select new
                                       {
                                           ProductStatus = c.new_ProductStatus,
                                           LineofBusiness = c.new_LineofBusiness
                                       };
                    var temp = saContext.Entities;
                    foreach (var c in query_where3)
                    {
                        System.Console.WriteLine("ProductStatus: " +
                         c.ProductStatus +
                         "\t\t\t" +
                         "LineofBusiness: " +
                         c.LineofBusiness);

                    }
                }
                //</snippetAuthenticateWithNoHelp3>

            }

MyOrganizationContext是在添加服务端点公开的CSDL文件时创建的上下文类

1 个答案:

答案 0 :(得分:0)

查看CRM Web Api预览:https://msdn.microsoft.com/en-us/dynamics/crm/webapipreview.aspx。您可以从xRM外部调用此端点,并且可以使用OAuth 2.0进行身份验证。