Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Authenticating to Microsoft Dynamic CRM

by rdfield (Priest)
on Mar 05, 2014 at 17:55 UTC ( #1077118=note: print w/replies, xml ) Need Help??

in reply to Authenticating to Microsoft Dynamic CRM

Having looked at various pieces of code in other languages (e.g. with and, I've cobbled this together, which logs into CRM Dynamics from Perl:
use strict; use warnings; use WWW::Curl::Easy; use Data::GUID; use Date::Manip; use XML::Simple; use Data::Dumper; sub make_guid { my $guidobj = Data::GUID->new; return $guidobj->as_string; } sub curl_request { my ($url,$data) = @_; my $headers = [ 'Connection: Keep-Alive', "Content-type: application/soap+xml; charset=UTF-8", "Content-length: " . length($data), ]; my $curl = WWW::Curl::Easy->new; $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_TIMEOUT, 60); $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); $curl->setopt(CURLOPT_FOLLOWLOCATION, 1); $curl->setopt(CURLOPT_SSLVERSION, 3); $curl->setopt(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); $curl->setopt(CURLOPT_HTTPHEADER, $headers); $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, $data); my $response_body; $curl->setopt(CURLOPT_WRITEDATA,\$response_body); my $retcode = $curl->perform; return $retcode, $response_body; } my $guid = make_guid; my $reqguid = make_guid; my $region = ''; # use your region my $username = 'youremailaddress'; my $password = 'yourpassword'; my $host = ""; # use crm5 etc depending on r +egion my $loginurl = ''; my $serviceurl = "https://$host/XRMServices/2011/Organization.svc"; my $OCPRequest = '<s:Envelope xmlns:s=" +envelope" xmlns:a="" xmlns:u="htt +p:// +ty-1.0.xsd"> <s:Header> <a:Action s:mustUnderstand="1"> +5/02/trust/RST/Issue</a:Action> <a:MessageID>urn:uuid:' . $guid . '</a:MessageID> <a:ReplyTo> <a:Address></a:A +ddress> </a:ReplyTo> <a:To s:mustUnderstand="1"> +2.srf</a:To> <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open"> <u:Timestamp u:Id="_0"> <u:Created>'. UnixDate(ParseDate("now"), "%Y-%m-%dT%H:%M:%S").' +Z</u:Created> <u:Expires>' . UnixDate(ParseDate("24 hours"), "%Y-%m-%dT%H:%M +:%S") . 'Z</u:Expires> </u:Timestamp> <o:UsernameToken u:Id="uuid-cdb639e6-f9b0-4c01-b454-0fe244de73 +af-1"> <o:Username>' . $username . '</o:Username> <o:Password Type=" +200401-wss-username-token-profile-1.0#PasswordText">' . $password . ' +</o:Password> </o:UsernameToken> </o:Security> </s:Header> <s:Body> <t:RequestSecurityToken xmlns:t=" +005/02/trust"> <wsp:AppliesTo xmlns:wsp=" +9/policy"> <a:EndpointReference> <a:Address>'. $region .'</a:Address> </a:EndpointReference> </wsp:AppliesTo> <t:RequestType> +ue</t:RequestType> </t:RequestSecurityToken> </s:Body> </s:Envelope>'; my ($retcode, $response); ($retcode, $response) = curl_request($loginurl, $OCPRequest); if ($retcode) { die("An error logging in\n"); } my $xml = XMLin($response); my $cipher1 = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse'}{'ws +t:RequestedSecurityToken'}{'EncryptedData'}{'ds:KeyInfo'}{'EncryptedK +ey'}{'CipherData' }{'CipherValue'}; my $cipher2 = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse'}{'ws +t:RequestedSecurityToken'}{'EncryptedData'}{ 'CipherData'}{'CipherVal +ue'}; my $keyidentifier = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse +'}{'wst:RequestedSecurityToken'}{'EncryptedData'}{ 'ds:KeyInfo'}{'Enc +ryptedKey'}{'ds:KeyInfo'}{'wsse:SecurityTokenReference'}{'wsse:KeyIde +ntifier'}{'content'}; my $action = "Execute"; # or Retrieve, Create etc my $envelope_start = ' <s:Envelope xmlns:s=" +velope" xmlns:a="" xmlns:u=" +200401-wss-wssecurity-utility-1.0.xsd">'; my $crmSoapRequestHeader = ' <s:Header> <a:Action s:mustUnderstand="1"> +rvices/IOrganizationService/' . $action . '</a:Action> <a:MessageID> urn:uuid:' . $reqguid . '</a:MessageID> <a:ReplyTo> <a:Address></ +a:Address> </a:ReplyTo> <a:To s:mustUnderstand="1"> ' . $serviceurl . '</a:To> <o:Security s:mustUnderstand="1" xmlns:o=" +sis-200401-wss-wssecurity-secext-1.0.xsd"> <u:Timestamp u:Id="_0"> <u:Created>' .UnixDate(ParseDate("now"), "%Y-% +m-%dT%H:%M:%S") . 'Z</u:Created> <u:Expires>' . UnixDate(ParseDate("24 hours"), + "%Y-%m-%dT%H:%M:%S") . 'Z</u:Expires> </u:Timestamp> <EncryptedData Id="Assertion0" Type="" xmlns=""> <EncryptionMethod Algorithm=" +/2001/04/xmlenc#tripledes-cbc"> </EncryptionMethod> <ds:KeyInfo xmlns:ds=" +9/xmldsig#"> <EncryptedKey> <EncryptionMethod Algorithm="http://www.w3"> </EncryptionMethod> <ds:KeyInfo Id="keyinfo"> <wsse:SecurityTokenReference xmlns:wsse= +" +ecext-1.0.xsd"> <wsse:KeyIdentifier EncodingType="http +:// +rity-1.0#Base64Binary" ValueType=" +wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIde +ntifier"> ' . $keyidentifier . ' </wsse:KeyIdentifier> </wsse:SecurityTokenReference> </ds:KeyInfo> <CipherData> <CipherValue> ' . $cipher1 . ' </CipherValue> </CipherData> </EncryptedKey> </ds:KeyInfo> <CipherData> <CipherValue> ' . $cipher2 . ' </CipherValue> </CipherData> </EncryptedData> </o:Security> </s:Header> '; my $retrieveMultipleRequestBodyTemplate =' <s:Body> <Execute xmlns=" +vices" xmlns:i=""> <request i:type="b:WhoAmIRequest" xmlns:a="http://schemas.microsof" xmlns:b=" +011/Contracts"> <a:Parameters xmlns:c=" +stem.Collections.Generic" /> <a:RequestId i:nil="true" /> <a:RequestName>WhoAmI</a:RequestName> </request> </Execute> </s:Body> '; my $envelope_end = ' </s:Envelope>'; ($retcode, $response) = curl_request($serviceurl,$envelope_start . $cr +mSoapRequestHeader . $retrieveMultipleRequestBodyTemplate . $envelope +_end); if ($retcode == 0) { print("Transfer went ok\n"); print("Received response: $response\n"); } else { print("An error happened: $retcode \n"); print $envelope_start . $crmSoapRequestHeader . $retrieveMultipleRe +questBodyTemplate . $envelope_end }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077118]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-02-28 19:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found