Authentication for CRM 4.0 has been a problem for me for the last few hours as I was trying to authenticate my custom aspx pages with CRM User. You are able to get the Current CRM User from a CRM form easily using the javascript WhoAmI function. But when it comes to the iframes or Custom pages you are using, the function does not work directly. In this article I will try to explain how to get the CRM scripts and apply them for your own webform.
First of all you are going to need some of the common CRM .js files ( didn’t have enough time to extract the exact functions and variables from the files, sorry for that ) which are ; encodedecode.js, global.js and help.js.
After you include these scripts in your head tag, you will have to write these functions below.
function GetCurrentUserInfo()
{
var SERVER_URL = "http://<%=GetCRMServer() %>";
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST", SERVER_URL + "/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
var soapBody = "<soap:Body>"+
"<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<Request xsi:type='WhoAmIRequest' />"+
"</Execute></soap:Body>";
var soapXml = "<soap:Envelope " +
"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "+
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "+
"xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";
soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "</soap:Envelope>";
xmlhttp.send(soapXml);
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlhttp.responseXML.xml);
var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
var buid = xmlDoc.getElementsByTagName("BusinessUnitId")[0].childNodes[0].nodeValue;
var orgid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue;
alert(userid);
}
This is the function that you need to use to get CRM User Id. Using the GetCRMServer function, you get the IP or address of your server. Instead, you can hard code it directly if you want.
However, there is still a problem. The GenerateAuthenticationHeader() function is coming from a common js file of CRM. But this time we will have to directly write some of the functionalities there. Which will make our function something like that:
function GenerateAuthenticationHeader()
{
var xml = new StringBuilder();
xml.Append("<soap:Header><CrmAuthenticationToken xmlns=\"");
xml.Append(CrmEncodeDecode.CrmXmlEncode("http\x3a\x2f\x2fschemas.microsoft.com\x2fcrm\x2f2007\x2fWebServices"));
xml.Append("\"><AuthenticationType xmlns=\"");
xml.Append(CrmEncodeDecode.CrmXmlEncode("http\x3a\x2f\x2fschemas.microsoft.com\x2fcrm\x2f2007\x2fCoreTypes"));
xml.Append("\">");
xml.Append(CrmEncodeDecode.CrmXmlEncode(0));
xml.Append("</AuthenticationType><CrmTicket xmlns=\"");
xml.Append(CrmEncodeDecode.CrmXmlEncode("http\x3a\x2f\x2fschemas.microsoft.com\x2fcrm\x2f2007\x2fCoreTypes"));
xml.Append("\"></CrmTicket><OrganizationName xmlns=\"");
xml.Append(CrmEncodeDecode.CrmXmlEncode("http\x3a\x2f\x2fschemas.microsoft.com\x2fcrm\x2f2007\x2fCoreTypes"));
xml.Append("\">");
xml.Append(CrmEncodeDecode.CrmXmlEncode("<%=GetOrganizationName() %>"));
xml.Append("</OrganizationName><CallerId xmlns=\"");
xml.Append(CrmEncodeDecode.CrmXmlEncode("http\x3a\x2f\x2fschemas.microsoft.com\x2fcrm\x2f2007\x2fCoreTypes"));
xml.Append("\">00000000-0000-0000-0000-000000000000</CallerId></CrmAuthenticationToken></soap:Header>");
return xml.ToString();
}
The GetOrganizationName function gets the Organization Name which is defined when CRM is installed. And our token is finally formed.
This function works correctly if your active directory user is defined in CRM. You can also get Buid and Organization Id using this function.
Popularity: 100% [?]
Tags: ASP.NET, CRM, Javascript, WhoAmI, XMLHTTP
slm bunu c# kodlar?yla ns?l yapabilirim.
Selam,
CRM içerisinde SystemUserBase / SystemUserExtensionBase tablolar?nda kullan?c? bilgileri tutulur.
E?er aspx sayfalar?nda windows authentication kullan?yorsan current user\’? alarak o windows domain\\kullan?c? ad?n? alarak bu tablolarda hangi CRM user\’a kar??l?k geldi?ini ö?renebilirsin.
Te?ekkürler..