Showing posts with label Login. Show all posts
Showing posts with label Login. Show all posts

Wednesday, April 13, 2011

MessageSecurityException when connecting to the Web Services

When trying to establish connection with the organization service or discovery service in CRM 2011 you may receive the following exception

Unhandled Exception: System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

Solution: You need to Delete the device registration folder named LiveDeviceID in your profile folder C:\Users\<username>.

Alternatively you can navigate to “C:\Users\<username>\LiveDeviceID\” and rename the LiveDevice.xml file to LiveDeviceOld.xml.

Tuesday, June 2, 2009

IFD Login code for Custom apps

With CRM 4.0 and IFD getting popular by the day. We have had to work on the Login function for CRM to support IFD install. In case of IFD the authentication type is changed to Forms Authentication where you are presented with a Sign-in page for login.

Further to our post on
Login for various CRM deployments here is the code for IFD that has been added to the list.

The code below should help users developing application that need to work from an IFD environment.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///


/// login to crm with Default Credential for IFD

//set orgname and url to properties
strOrg = strOrgName;
strserverurl = strCRMURL; // It must have on- premise url for example http://moss:5555

//init CrmImpersonator object
_impersonator = new CrmImpersonator();

//Initialize CrmAuthenticationToken token
token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(context, strOrgName);
token.OrganizationName = strOrgName;
token.AuthenticationType = 0;

//initialize the Service
service = new CrmService();

//pass DefaultCredentials
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

//pass CrmAuthenticationTokenValue
service.CrmAuthenticationTokenValue = token;
service.Url = strCRMURL + "/mscrmservices/2007/crmservice.asmx";


Thursday, March 5, 2009

How to Login to various CRM deployments

CRM supports various Deployment methods and unfortunately for the developers the method to connect to each of these differs. So here are the login functions for each of the Deployment methods supported by Dynamics CRM.

On Premise CRM connection:

On premise uses AD for authentication.

CrmService service = new CrmService();

service.Url = "http://" + servername + "/mscrmservices/2007/crmservice.asmx";

service.CrmAuthenticationTokenValue.OrganizationName = "orgname";

service.CrmAuthenticationTokenValue.AuthenticationType = 0;

service.Credentials = new System.Net.NetworkCredential("username", "Password", "DomainName");

OR

crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;


Live CRM connection:

CRM Live uses Passport authentication. It requires a reference to the Idcrlwrapper be added. The Idcrlwrapper is available along with the SDK as helper files.

Make sure you have copied the msidcrl40.dll to the system32 folder before you try to run this code.

CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.Url = "https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx";

RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();

RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);

// Retrieve a Windows Live ticket from the Live
service.LogonManager lm = new LogonManager();

string passportTicket = lm.Logon("LiveWindowsUserId", "password", "crm.dynamics.com", policyResponse.Policy, "Production");

RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();

crmTicketRequest.OrganizationName = "OrganizationName";

crmTicketRequest.PassportTicket = passportTicket;

RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.AuthenticationType = 1;

token.CrmTicket = crmTicketResponse.CrmTicket;

token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;

Now, once you have the token assign it to the crmservice object as we do in on premise.

Hosted CRM connection:

This uses SPLA authentication. You need to use the userid and password provided to you by your hosting provider. You would also need to get the discovery URL from them.

From the discovery service URL you can retrieve the list of organizations.

RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();

orgRequest.UserId = username;

orgRequest.Password = password;

RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);

//Find the desired organization with which you want to connect.
//Retrieve the ticket.

RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();

ticketRequest.OrganizationName = organization;

ticketRequest.UserId = username;

ticketRequest.Password = password;

RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);

//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();

sdktoken.AuthenticationType = 2;

sdktoken.OrganizationName = organization;

sdktoken.CrmTicket = ticketResponse.CrmTicket;

Now, once you have the token assign it to the crmservice object as we do in on premise.