1

I'm looking for a way to authenticate a user (given a username and password) via the Microsoft CRM 4.0 Web Services API. Ideally, I'd like to filter down a list of projects based on which ones the logged in user has access to. i may be able to figure out the second part but I can't find a way to authenticate the user. The way all of the cals are currently made in the web service is via:

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");

Then calls can be made via the service. I guess I could potentially try to authenticate to CRM via the user's username/password but it feels wrong somehow.

Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
Echilon
  • 10,064
  • 33
  • 131
  • 217

1 Answers1

1

If you are in an on-premise environment, you should be able to use the following code to get a valid CRM service that can be used to retrieve your projects.

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;
}
cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • 1
    Hi cchamberlain, I have a similar issue and this code works for my on premise but I am attempting to contact my crm server from a website also hosted in azure using the cloudapp.net URL of the crm server but the AD method does not seem to be working despite passing the AD details across – Jay Aug 09 '16 at 11:40
  • 1
    @Jay Sorry, I've been out of the dynamics spectrum for 4 years or so. I'd guess that it is because you are trying to AD authenticate from a cloud app to your local AD from a non-domain machine. The code above relies on your machine being on the domain (network credential). My assumption is you'd have to use their OAuth API to communicate with CRM cross-domain. https://msdn.microsoft.com/en-us/library/gg327838.aspx – cchamberlain Aug 10 '16 at 14:23