编程语言
首页 > 编程语言> > c#-如何使用SDK连接到CRM(基于声明的身份验证和自定义STS)

c#-如何使用SDK连接到CRM(基于声明的身份验证和自定义STS)

作者:互联网

我在CRM实例上配置了基于声明的身份验证.我正在使用自定义STS(示例可用here)现在,我想从某些测试应用程序访问Web服务.
有人对此有一些例子吗?
在Windows身份验证的情况下,我尝试使用相同的代码进行连接.但是,当然不成功.
我收到一个错误:

{“The authentication endpoint Kerberos was not found on the configured
Secure Token Service!”}

这是用于连接的代码(用于AD身份验证类型):

OrganizationServiceProxy orgserv;
      ClientCredentials clientCreds = new ClientCredentials();
      ClientCredentials devCreds = new ClientCredentials();


        clientCreds.Windows.ClientCredential.UserName = "user";
        clientCreds.Windows.ClientCredential.Password = "P@$$w0rd";
        clientCreds.Windows.ClientCredential.Domain = "myDomain";
        IServiceConfiguration<IOrganizationService> orgConfigInfo =
                    ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri("https://myCRMServer/myOrg/XRMServices/2011/Organization.svc"));

        using (orgserv = new OrganizationServiceProxy(orgConfigInfo, clientCreds))
        {
          orgserv.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
          orgserv.EnableProxyTypes();
          connection = orgserv;
        }

我发现某个地方对于基于声明的身份验证足以仅发送UPN(用户主体名称).但是,同样的错误也会发生.我也尝试使用用户名/密码组合,但未成功.

AuthenticationCredentials authCredentials = new AuthenticationCredentials();

authCredentials.UserPrincipalName = "user";

authCredentials.ClientCredentials.UserName.UserName = _userName;
authCredentials.ClientCredentials.UserName.Password = _password;

这之后的错误是:在已配置的安全令牌服务上找不到身份验证终结点用户名!

解决方法:

如果您仅使用CRM 2011 Web服务界面,我认为索赔并不重要.以下代码允许进行身份验证并连接到CRM 2011并使用REST API

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace CRM_REST_FromConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var url = new Uri(@"https://MyServer/MyOrganiation/xrmservices/2011/organizationdata.svc/AccountSet?$select=Name&$top=10");

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            //TODO: Set Credentials Here            
            request.Credentials = new NetworkCredential("USERNAME GOES HERE", "PASSWORD GOES HERE", "myDomain");


            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());

                Console.WriteLine(reader.ReadToEnd());
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
} 

标签:dynamics-crm,dynamics-crm-2011,claims-based-identity,sts-securitytokenservice,c
来源: https://codeday.me/bug/20191101/1980321.html