使用MS Crm Web Service进行身份验证

时间:2011-06-10 08:41:38

标签: web-services crm dynamics-crm-4 service-tier

我正在寻找通过Microsoft CRM 4.0 Web Services API对用户进行身份验证(给定用户名和密码)的方法。理想情况下,我想根据登录用户可以访问的项目列表来过滤项目列表。我可能能够弄清楚第二部分,但我找不到验证用户身份的方法。当前在Web服务中创建所有cals的方式是:

MyWebServices.CrmService svc = new MyWebServices.CrmService();
MyWebServices.CrmAuthenticationToken token = new MyWebServices.CrmAuthenticationToken();
token.OrganizationName = "MyCRM";
token.AuthenticationType = 0;
svc.CrmAuthenticationTokenValue = token;
svc.PreAuthenticate = true;
svc.Credentials = System.Net.CredentialCache.DefaultCredentials;
svc.Credentials = new NetworkCredential("hj", "mypass", "mydomain");

然后可以通过该服务进行呼叫。我想我可能会尝试通过用户的用户名/密码对CRM进行身份验证,但不知何故感觉不对。

1 个答案:

答案 0 :(得分:1)

如果您处于内部部署环境中,则应该能够使用以下代码获取可用于检索项目的有效CRM服务。

public static Microsoft.Crm.SdkTypeProxy.CrmService GetCrmService(string crmServerUrl, string organizationName, System.Net.NetworkCredential networkCredential)
{
    // Setup the Authentication Token
    CrmAuthenticationToken crmAuthenticationToken = new CrmAuthenticationToken
                                           {
                                               OrganizationName = organizationName,
                                               AuthenticationType = 0
                                           };

    var crmServiceUriBuilder = new UriBuilder(crmServerUrl) { Path = "//MSCRMServices//2007//CrmService.asmx" };

    // Instantiate a CrmService
    var crmService = new Microsoft.Crm.SdkTypeProxy.CrmService
    {
        Url = crmServiceUriBuilder.ToString(),
        UseDefaultCredentials = false,
        Credentials = networkCredential,
        CrmAuthenticationTokenValue = crmAuthenticationToken
    };

    return crmService;
}