Friday 15 June 2012

CRM 2011 Online Authentication for Custom Web application

Hi everyone,

Long time no post.

Today I would like to show you guys how to authentication CRM 2011 Online in coding .Net C#.
The objective is: creating a custom web application capturing contact information, then updating the information into CRM Online.

For CRM On Premise, to do so, you guys could follow exactly the same to this blog: http://blogs.msdn.com/b/crminthefield/archive/2011/06/03/microsoft-dynamics-crm-2011-custom-contact-entry-website-using-early-bound-entity-classes.aspx

However, in order to connect to CRM 2011 Online using IOrganizationService Web Service, you need to change the code a bit to make the authentication through (Remember, CRM On Premise using AD authentication, but CRM Online using Windows Live ID to do that).

So please, following the above blog until the code inside and using the below code for CRM Online Authentication:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm;
using Microsoft.Crm.Tools;
using System.Net;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Services.Utility;
namespace CRM_Test2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        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 = "abc@hotmail.sg";   //your Windows Live ID
            string Password = "crmadmin@123";    // 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://<CRM-Online-Server-Name>/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;
          
            //Instantiate the contact object and populate the attributes.                     
            Contact contact = new Contact();
            contact.FirstName = txtFirstName.Text.ToString();
            contact.LastName = txtLastName.Text.ToString();
            contact.EMailAddress1 = txtEmailAddress.Text.ToString();
            Guid newContactId = service.Create(contact);           
         
            //This code will clear the textboxes after the contact is created.
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtEmailAddress.Text = "";
            txtPhoneNumber.Text = "";

            }
        }
       
    }
}

Notice the red lines in above code. That's the difference between authenticate in CRM Online and On Premise. For Online version, you need to call the function named "GetDeviceCredentials()", which is defined as private function before that. In order to use this function, you MUST to add one more item which is from SDK --> SampleCode --> Cs --> deviceidmanager.cs. So add this new item into your solution.

After adding the deviceidmanager.cs item, open the cs file and add one more nesscessary reference for using the item, which is: System.Security.Cryptography.

Then now build the whole solution ! Everything is done !

Any question or unclear point. Please drop me an email at linhhk87@gmail.com. I will reply you very quick ;)

Have a nice weekend ;)

26 comments:

  1. Thank you so much for this review! I found it very helpful, this seem like a program that would be of great use to me. Keep it up!

    Arabia Interactive - Web Design Dubai

    ReplyDelete
  2. Hi, thanks for your comment. Merry Xmas & Happy New Year :)

    ReplyDelete
  3. I could not run, I mark that is not the library and not Microsoft.Crm.Services.Utility Contact (), how I can fix this, I would appreciate a prompt response

    ReplyDelete
  4. I read your all contain it is very helpful for your service , find some information related custom web applications

    ReplyDelete
  5. Hi kelly,

    I am doing this from my local machine (Windows 7 Os). I have Asp.net website and did the same what you have mentioned above. But while running i m getting this error.

    The authentication endpoint Username was not found on the configured Secure Token Service!".

    I m connecting online CRM and added 'DeviceId" class from helper code to my website.

    ReplyDelete
  6. It was very helpful. Thanks.

    ReplyDelete
  7. Hi,
    I've done everything that you said in the article and then deployed it to my web host. It complains that it cant find the microsoftidentitymodel assembly

    I've now asked my provider if they can install this for me (as I dont have the rights to install it it) - failing that is there anything I can do to get around this (a different way of connecting to an online instance from a web sever - not localhost)?

    Thanks.

    ReplyDelete
  8. The code is also suitable for on-premise after setup claims-based authentication (IFD).

    Thanks. you saved me a lot of time.

    ReplyDelete
  9. Great article and interesting discovery about computer services.

    Windows Thin Client & Zero Clients

    ReplyDelete
  10. This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.

    Digital Graphic Design & Company Profile Design

    ReplyDelete
  11. i just wanna thank you for sharing your information and your site or blog this is simple but nice article I've ever seen i like it i learn something today...

    pay per click agency & online marketing company

    ReplyDelete
  12. Very Nice blog. I am really happy while reading it.

    ReplyDelete
  13. Well the code you have written in ASP.Net is really helpful and I haven’t tried it so I look forward to it. I will ask query because I am learning .Net now a days and it is very powerful language.

    Software for real estate agents

    ReplyDelete
  14. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

    search Engine Optimization services in Mumbai & Social Media Optimization Company in Mumbai

    ReplyDelete
  15. It gives a robust tool for handling all the activities of a business.....
    CRM Software for Real Estate Agents

    ReplyDelete
  16. This software works on a common goal to enable your sales and marketing....
    CRM Solutions Real Estate Developers

    ReplyDelete
  17. This information is impressive; I am inspired with your post writing style & how continuously you.

    Web Development Dubai

    ReplyDelete
  18. Thank you so much for this review! I found it very helpful, this seem like a program that would be of great use to me. Keep it up! web designer Dubai

    ReplyDelete
  19. Nice Information thank you for Sharing useful information.
    Custom Web Application Development

    ReplyDelete
  20. Thanks for this worth reading post. I really enjoyed it and surely will also recommend others.
    SEO Sharjah

    ReplyDelete
  21. Your blog is great and now I have become a regular visitor of your blog. I appreciate the good work.
    Public Relation company in Dubai

    ReplyDelete
  22. Thank you for providing a wonderful article. It is a great guide for me. Please share more articles like this.

    ReplyDelete
  23. Splendid post. Also visit here: Feeta.pk property for sale in islamabad . it is very helpful for us thanks for sharing.

    ReplyDelete