无法在访问动态CRM在线Web服务时进行身份验证



我需要利用公开的Dynamic CRM Data Service Endpoint从其中一个方法获取数据。服务(microsoft)帐户有权访问此服务。我已经尝试使用这里提供的示例代码[https://msdn.microsoft.com/en-us/library/hh675404.aspx]验证发现服务和组织服务,并且成功了。然而,我不能使用相同的身份验证来访问数据服务,因为我可以找到与其他两个相关的数据服务。使用网络凭证进行基本身份验证不起作用。

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

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

谁能提出一种方法来验证用户访问数据服务?

添加代码:

//<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 +
                         "ttt" +
                         "LineofBusiness: " +
                         c.LineofBusiness);
                    }
                }
                //</snippetAuthenticateWithNoHelp3>
            }

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

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

最新更新