0
votes

I'm trying to connect to a CRM 2011 Online environment. I'm able to connect via a "Console Application", but when I'm trying to connect via an "ASP.net"-application with the same code, it doesn't work, it gives me the "Authentication Failure"-error ({"An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail."}).

Is there something special we need to do to make it work on an "ASP.net" environment. I tested out several solutions I found on the internet, but all gives me the same error.

A "code"-snippet of my simplified code:

    private static ClientCredentials GetDeviceCredentials()
    {
        return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Authenticate using credentials of the logged in user;
        string UserName = "*****";   //your Windows Live ID
        string Password = "*****";    // your password
        ClientCredentials Credentials = new ClientCredentials();
        Credentials.UserName.UserName = UserName;
        Credentials.UserName.Password = Password;

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        //This URL needs to be updated to match the servername and Organization for the environment.
        Uri OrganizationUri = new Uri("https://*****.crm4.dynamics.com/XRMServices/2011/Organization.svc");           //this URL could copy from Setting --> Developer Source 

       Uri HomeRealmUri = null;
        //OrganizationServiceProxy serviceProxy;   
       using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, GetDeviceCredentials()))
       {
           IOrganizationService service = (IOrganizationService)serviceProxy;
           OrganizationServiceContext orgContext = new OrganizationServiceContext(service);

           var theAccounts = orgContext.CreateQuery<Account>().Take(1).ToList();
           Response.Write(theAccounts.First().Name);
       }

    }

I tried several things, like deleting the content of "LiveDeviceID"-folder an re-running the device registration tool. but is weird that it works in the "console application" but not on my "asp.net"-solution...

PS : I am able to generate the "context"-file via crmsvcutil.exe /url:https://org.crm4.dynamics.com/XRMServices/2011/Organization.svc /o:crm.cs /u:username /p:password /di:deviceUserName /dp:devicPWD

1
remember that the device credentials depend where your code is running. If inside the server where is published your asp.net web application the device credential cannot be generated (permission issues for example) the code will not work (but maybe will throw an exception, I'm not sure). Btw, your organization still use liveid or is office365?Guido Preite
What I don't understand, that it works for the "console application" but it doesn't work for the "ASP.net". I'm running my ASP.net locally and the same device credentials are used for both codes. When i'll put my asp.net online, I'll have to register the device their again I guess.Freeetje

1 Answers

0
votes

Is there any particular reason you have

Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

You shouldn't need that line for windows live authentication.

Even with that the code seems valid so it is something to do with the Device Registration. I suggest rather than just call it directly like you have

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, GetDeviceCredentials()))
   {

You try something like the following because you only need to register once:

ClientCredentials deviceCredentials;

if ((CRMSettings.Default.DeviceID == String.Empty) || (CRMSettings.Default.DevicePassword == String.Empty))
{
    deviceCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.RegisterDevice();
}
else
{
    deviceCredentials = new ClientCredentials();
    deviceCredentials.UserName.UserName = CRMSettings.Default.DeviceID;
    deviceCredentials.UserName.Password = CRMSettings.Default.DevicePassword;
}

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, deviceCredentials))
{

I have had issues in the past where I get an "already registered" response from the RegisterDevice call.

I would also dump out the Device ID and Password so you can see if they are being set.