コード例 #1
0
        public ActionResult Login(CrmAccountViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            IOrganizationService organizationService = null;

            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = vm.UserIdEmail;
                clientCredentials.UserName.Password = vm.Password;

                // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
                // Copy and Paste Organization Service Endpoint Address URL
                organizationService = (IOrganizationService) new OrganizationServiceProxy(new Uri(vm.CrmWebServiceUrl), //"https://"+ vm.CrmWebService + ".api.crm4.dynamics.com/XRMServices/2011/Organization.svc"),
                                                                                          null, clientCredentials, null);

                if (organizationService != null)
                {
                    Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

                    if (userid != Guid.Empty)
                    {
                        Session["userId"]         = userid;
                        Session["username"]       = clientCredentials.UserName.UserName;
                        ViewBag.ConnectionSuccess = "Connection Established Successfully...";
                        return(RedirectToAction(nameof(Index)));
                    }
                }
                else
                {
                    ViewBag.FailedConnection = "Failed to Established Connection!!!";
                    return(View(vm));
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("Exception caught - " + ex.Message);
                ViewBag.Wrong = "Error in login.";
                return(View(vm));
            }

            // return RedirectToAction(nameof(Index));
            return(View(vm));
        }
コード例 #2
0
        public List <CrmAccountViewModel> GetAllAccounts()
        {
            var accounts = _context.CrmAccounts.ToList();
            List <CrmAccountViewModel> acList = new List <CrmAccountViewModel>();

            foreach (var account in accounts)
            {
                var crmAccountVm = new CrmAccountViewModel
                {
                    AccountId     = account.AccountId,
                    AccountName   = account.AccountName,
                    AccountNumber = account.AccountNumber,
                    VatNumber     = account.VatNumber
                };
                acList.Add(crmAccountVm);
            }
            return(acList);
        }