private CrmAuthenticationToken GetCrmAuthenticationToken(IAuthenticationRequest tokenRequest, RetrieveCrmTicketResponse ticket) { Int32 authenticationType; #if DEBUG_AuthOverride authenticationType = Globals.CrmServiceSettings.DeploymentType; #else if (String.IsNullOrEmpty(tokenRequest.DomainName)) { // this is a CRM user, don't authenticate with their credentials authenticationType = Globals.CrmServiceSettings.DeploymentType; } else { // this is a domain user, authenticate via AD authenticationType = AuthenticationType.AD; } #endif CrmAuthenticationToken sdkToken = new CrmAuthenticationToken() { AuthenticationType = authenticationType, OrganizationName = ticket.OrganizationDetail.OrganizationName, CrmTicket = ticket.CrmTicket }; return(sdkToken); }
public Token RequestToken(IAuthenticationRequest tokenRequest) { // Check that we're accessing a valid organization from the CrmDiscoveryService Web service. if (String.IsNullOrEmpty(tokenRequest.OrganizationName)) { OrganizationDetail orgInfo = GetOrganizationDetail(); tokenRequest.OrganizationName = orgInfo.OrganizationName; } // Retrieve a CrmTicket from the CrmDiscoveryService Web service. RetrieveCrmTicketResponse crmTicketResponse = GetCrmTicketResponse(tokenRequest); if (crmTicketResponse == null) { // user not found, try default credentials IAuthenticationRequest defaultTicketRequest = GetDefaultTicketRequest(); crmTicketResponse = GetCrmTicketResponse(defaultTicketRequest); } // user not found at all, throw exception if (crmTicketResponse == null) { InvalidCredentialsException ex = new InvalidCredentialsException(); ex.Data.Add("Username", tokenRequest.DomainName + @"\" + tokenRequest.Username); throw ex; } CrmAuthenticationToken crmToken = GetCrmAuthenticationToken(tokenRequest, crmTicketResponse); return(new Token(crmToken, crmTicketResponse.OrganizationDetail.CrmServiceUrl)); }
/// <summary> /// Initializes a new instance of the <see cref="CrmService"/> class. /// </summary> /// <param name="connecitonString">The conneciton string.</param> public CrmService(string connecitonString) { NameValueCollection parameters = CrmRemoteSettings.GetServiceParameters(connecitonString); if (parameters != null && parameters.Count > 0) { if (!string.IsNullOrEmpty(parameters[CrmRemoteSettings.CrmUrlKey])) { Url = parameters[CrmRemoteSettings.CrmUrlKey].ToLower().Replace(@"/2006/", @"/2007/"); } if (!string.IsNullOrEmpty(parameters[RemoteSettings.UserId])) { var serviceCredentials = SecurityUtil.CreateNetworkCredential(parameters[RemoteSettings.UserId], parameters[RemoteSettings.PasswordKey]); Credentials = serviceCredentials; ConnectionGroupName = SecurityUtil.CreateSecureGroupName(serviceCredentials); CrmAuthenticationTokenValue = new CrmAuthenticationToken { AuthenticationType = SecurityUtil.GetAuthenticationType( parameters[RemoteSettings.AuthenticationType]), OrganizationName = parameters[RemoteSettings.OrganizationName] }; if (MainUtil.GetBool(parameters[RemoteSettings.UseTicket], false)) { var ticket = CrmTicketManager.Provider.GetTicket(Url, parameters[RemoteSettings.UserId], parameters[RemoteSettings.PasswordKey], parameters[RemoteSettings.OrganizationName], parameters[RemoteSettings.PartnerName], parameters[RemoteSettings.EnvironmentName], CrmAuthenticationTokenValue.AuthenticationType); if (ticket != null) { CrmAuthenticationTokenValue.CrmTicket = ticket.Value; CrmAuthenticationTokenValue.OrganizationName = ticket.OrganizationName; expirationDate = ticket.ExpirationDate; } } } } else { expirationDate = DateTime.MinValue; } }
private void _init() { var token = new CrmAuthenticationToken(); token.AuthenticationType = 0; //token.OrganizationName = "pccrm"; token.OrganizationName = "testenvironment"; Console.WriteLine("Connecting to CRM: " + token.OrganizationName); var service = new SdkTypeProxy.CrmService(); service.Url = "http://pci-app/mscrmservices/2007/crmservice.asmx"; service.CrmAuthenticationTokenValue = token; service.Credentials = System.Net.CredentialCache.DefaultCredentials; _service = service; }
private static CrmService CreateServiceInstance(IAuthenticationRequest authRequest) { //took out discovery service as the backward compatible discovery service does not work well with ADFS. CrmService serviceClient; using (TokenBroker tokenBroker = new TokenBroker()) { OrganizationDetail orgInfo = tokenBroker.GetOrganizationDetail(); serviceClient = new CrmService() { Url = orgInfo.CrmServiceUrl//,.ServiceUrl, //CrmAuthenticationTokenValue = orgInfo.CrmToken }; switch (Globals.CrmServiceSettings.DeploymentType) { case AuthenticationType.AD: CrmAuthenticationToken ADToken = new CrmAuthenticationToken(); ADToken.AuthenticationType = 0; //ActiveDirectory ADToken.OrganizationName = orgInfo.OrganizationName; serviceClient.CrmAuthenticationTokenValue = ADToken; serviceClient.UnsafeAuthenticatedConnectionSharing = true; serviceClient.PreAuthenticate = true; //serviceClient.UseDefaultCredentials = true; //serviceClient.Credentials = System.Net.CredentialCache.DefaultCredentials; serviceClient.Credentials = new System.Net.NetworkCredential(authRequest.Username, authRequest.Password, authRequest.DomainName); //serviceClient.CrmAuthenticationTokenValue.CallerId = new Guid("97945673-0ac2-e111-9748-000423c7d319"); //serviceClient.PreAuthenticate = true; break; default: TokenBroker.Token token = tokenBroker.RequestToken(authRequest); serviceClient.CrmAuthenticationTokenValue = token.CrmToken; break; } } authRequest.WasRefreshed = true; return(serviceClient); }
/// <summary> /// Method to create an instance of the /// Webservice based on the login criteria /// specified by the user /// </summary> /// <returns></returns> public CrmService GetCrmService() { CrmAuthenticationToken loToken = new CrmAuthenticationToken(); loToken.AuthenticationType = 0; loToken.OrganizationName = txtOrganization.Text; CrmService loService = new CrmService(); loService.CrmAuthenticationTokenValue = loToken; System.Net.NetworkCredential loCredentials = new System.Net.NetworkCredential(); loCredentials.Domain = txtDomain.Text; loCredentials.UserName = txtUsername.Text; loCredentials.Password = txtPassword.Text; loService.Credentials = loCredentials; loService.CrmAuthenticationTokenValue = loToken; loService.Url = "http://" + txtServer.Text + ":" + txtPort.Text + "/mscrmservices/2007/crmservice.asmx"; return(loService); }
/// <summary> /// Метод создает новую запись сущности "contact". /// </summary> /// <param name="contactModel">Объект модели контакта.</param> public void AddContactToCrm(ContactEntityModel contactModel) { // Получаем токен. var token = new CrmAuthenticationToken { AuthenticationType = 0, OrganizationName = "AdventureWorksCycle" }; var connectionString = System.Configuration.ConfigurationManager. ConnectionStrings["CrmConnection"].ConnectionString; // Получаем сервис организации CRM. var service = new CrmService { Url = connectionString, CrmAuthenticationTokenValue = token, Credentials = System.Net.CredentialCache.DefaultCredentials }; // Создаем новую запись. var newContact = new contact { firstname = contactModel.ContactFirstName, lastname = contactModel.ContactLastName, mobilephone = contactModel.MobilePhone, emailaddress1 = contactModel.Email, jobtitle = contactModel.JobTitle }; if (contactModel.BirthDay.HasValue) { newContact.birthdate = CrmDateTime.FromUser(contactModel.BirthDay.Value); } service.Create(newContact); }
public bool Run() { try { // Set up the CRM Service. CrmService service = new CrmService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; CrmAuthenticationToken token = new CrmAuthenticationToken(); // TODO Replace 'AdventureWorksCycle' with your Microsoft CRM organization name. token.OrganizationName = "AdventureWorksCycle"; service.CrmAuthenticationTokenValue = token; // TODO Replace 'localhost:5555' with your CRM server information. service.Url = @"http://*****:*****@" <importexportxml> <entities> <entity>new_bankaccount</entity> <entity>new_safedepositbox</entity> </entities> <nodes/> <securityroles/> <settings/> <workflows/> </importexportxml>" ; // Execute the import. ImportXmlResponse importResponse = (ImportXmlResponse)service.Execute(importRequest); #endregion #region Publish new entities // Create the request. PublishXmlRequest publishRequest = new PublishXmlRequest(); // Supply the request that is being published with the Bank Account and Safe Deposit Box. publishRequest.ParameterXml = @"<importexportxml> <entities> <entity>new_bankaccount</entity> <entity>new_safedepositbox</entity> </entities> <nodes/> <securityroles/> <settings/> <workflows/> </importexportxml>" ; // Execute the request. PublishXmlResponse publishResponse = (PublishXmlResponse)service.Execute(publishRequest); #endregion } catch (System.Web.Services.Protocols.SoapException) { // Add your error handling code here. } return(true); }
public Token(CrmAuthenticationToken crmToken, String serviceUrl) { _crmToken = crmToken; _serviceUrl = serviceUrl; }
public bool Run() { try { string configFileName = "demo.ini"; IniConfigSource configSource = new IniConfigSource(configFileName); IConfig demoConfigSection = configSource.Configs["Demo"]; string extractPath = demoConfigSection.Get("extractPath", string.Empty); // Set up the CRM Service. CrmService service = new CrmService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; CrmAuthenticationToken token = new CrmAuthenticationToken(); // TODO Replace 'AdventureWorksCycle' with your Microsoft CRM organization name. token.OrganizationName = "IBMRational"; service.CrmAuthenticationTokenValue = token; // TODO Replace 'localhost:5555' with your CRM server information. service.Url = @"http://*****:*****@"C:\Users\rsdiasoliveira\Downloads\sdk4\sdk\server\fullsample\importexportpublish\cs\bin\Debug\"; //const string customizationPath = @"C:\AppProject\Smart\ExternalSmartStaff\site\document"; Console.WriteLine(customizationPath); foreach (var file in Directory.GetFiles(customizationPath)) { File.Copy(file, Path.Combine(sourceDir, Path.GetFileName(file)), true); } // Pass the stream of customization XML to the import request. using (StreamReader sr = new StreamReader(Path.Combine(customizationPath + "customizations.xml"))) { string customizationXml = sr.ReadToEnd(); importRequest.CustomizationXml = customizationXml; } // Supply the request that is being imported with the Bank Accounts and Safe Deposit Boxes. // Supply the request that is being imported with the Bank Accounts and Safe Deposit Boxes. importRequest.ParameterXml = @" <importexportxml> <entities> <entity>contact</entity> <entity>incident</entity> <entity>credifin_campanhapromo</entity> </entities> <nodes/> <securityroles/> <settings/> <workflows/> </importexportxml>" ; // Execute the import. ImportXmlResponse importResponse = (ImportXmlResponse)service.Execute(importRequest); #endregion #region Publish new entities // Create the request. PublishXmlRequest publishRequest = new PublishXmlRequest(); // Supply the request that is being published with the Bank Account and Safe Deposit Box. publishRequest.ParameterXml = @"<importexportxml> <entities> <entity>contact</entity> <entity>incident</entity> <entity>credifin_campanhapromo</entity> </entities> <nodes/> <securityroles/> <settings/> <workflows/> </importexportxml>" ; // Execute the request. PublishXmlResponse publishResponse = (PublishXmlResponse)service.Execute(publishRequest); #endregion } catch (System.Web.Services.Protocols.SoapException) { // Add your error handling code here. } return(true); }