G.A.Alderman
G.A.Alderman

Reputation: 75

Log in to CRM from ASP.NET

I'm writing an application in which I have to log on to a CRM 2011 server from ASP.NET code. I quickly found this article:

http://msdn.microsoft.com/en-us/library/cc156363.aspx

The problem I'm having is in this bit of code from that article:

//Create the Service
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.CrmAuthenticationTokenValue = token;
service.Url = crmurl;

Visual Studio can't resolve CrmService. So I tried to add a web reference to this project and point the web reference at the CRM service I'm using. The URL I'm getting from Settings->Customizations in CRM, and I'm using the Organization Service endpoint. However, after I add that reference CrmService is still unresolvable. What am I doing wrong?

Upvotes: 1

Views: 1309

Answers (3)

James Wood
James Wood

Reputation: 17552

First off, you have linked a CRM 4 MSDN article, some things have changed so you might want try this one instead: Authenticate Users with Microsoft Dynamics CRM Web Services.

Then as an alternative you may want to try the CrmConnection class, its a helper library in Microsoft.Xrm.Client. It means you can use a connection string approach to authenticate with CRM (and let the class takes care of all the hard work).

var connection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso; Domain=CONTOSO; Username=jsmith; Password=passcode;");
var service = new OrganizationService(connection);
var context = new CrmOrganizationServiceContext(connection);

You can also keep the connection strings in config files makes life significantly easier.

Related articles:

Upvotes: 1

Konrad Viltersten
Konrad Viltersten

Reputation: 39058

Actually, the login procedure is heavily dependent on the authentication provider you're targeting. I'm currently in the process of structuring that info in a pedagogic way on my blog so you're welcome to check it out and nag if it's too techy.

There are at the moment four such ways.

  • Active directory
  • Live id
  • Federation
  • Online federation

Which is applicable in your case, you should know already. If not, there's code for that too uploaded just a few days ago.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
...
public AuthenticationProviderType GetAuthenticationProviderType(Uri address)
{
  IServiceManagement<IOrganizationService> organizationServiceManagement
    = ServiceConfigurationFactory.CreateManagement
      <IOrganizationService>(address);
  return organizationServiceManagement.AuthenticationType;
}

Assuming that you're aiming for AD, you're in luck. It's the easiest.

Uri organizationUrl = new Uri("http ... Organization.svc");
OrganizationServiceProxy organizationService = new OrganizationServiceProxy(
  organizationUrl, null, null, null);

If you're aiming for Live Id - that's stingy. I'm still trying to set up a graspable example. The ones at MSDN are just too heavy and confusing. At least when one's dense and lazy like me. More info at mentioned but undisclosed location.

Upvotes: 0

Daryl
Daryl

Reputation: 18895

If you're using standard AD authentication with a local environment this answer should work fine: How to Authenticate to CRM 2011?

Upvotes: 0

Related Questions