public async Task <ActionResult> StartNewKeyPairAsync(
            CreateNewKeyPairRequestFormApiModel request,
            string add,
            string del)
        {
            AuthorizeClient();
            UpdateApiModel(request);
            if (ModelState.IsValid &&
                String.IsNullOrEmpty(add) &&
                String.IsNullOrEmpty(del))
            {
                string id;
                try
                {
                    id = await _opcVault.CreateNewKeyPairRequestAsync(request);
                }
                catch (Exception ex)
                {
                    ViewData["ErrorMessage"] =
                        "Failed to create Certificate Request.\r\n" +
                        "Message:" + ex.Message;
                    goto LoadAppAndView;
                }
                string errorMessage = null;
                try
                {
                    // TODO: call depending on auto approve setup
                    //await opcVault.ApproveCertificateRequestAsync(id, false);
                }
                catch (Exception ex)
                {
                    errorMessage =
                        "Failed to approve Certificate Request.\r\n" +
                        "Please contact Administrator for approval." +
                        ex.Message;
                }
                return(RedirectToAction("Details", new { id, errorMessage }));
            }

            if (!String.IsNullOrWhiteSpace(request.DomainNames.Last()) &&
                !String.IsNullOrEmpty(add))
            {
                request.DomainNames.Add("");
            }

LoadAppAndView:
            // reload app info
            var application = await _opcVault.GetApplicationAsync(request.ApplicationId);

            if (application == null)
            {
                return(new NotFoundResult());
            }

            ViewData["Application"] = application;

            return(View(request));
        }
 private void UpdateApiModel(CreateNewKeyPairRequestFormApiModel request)
 {
     if (request.DomainNames != null)
     {
         request.DomainNames = request.DomainNames.Where(x => !string.IsNullOrEmpty(x)).ToList();
     }
     else
     {
         request.DomainNames = new List <string>();
     }
     if (request.DomainNames.Count == 0)
     {
         request.DomainNames.Add("");
     }
 }
        public async Task <ActionResult> StartNewKeyPairAsync(string id)
        {
            AuthorizeClient();
            var groups = await _opcVault.GetCertificateGroupsConfigurationAsync();

            if (groups == null)
            {
                return(new NotFoundResult());
            }

            string defaultGroupId, defaultTypeId;

            if (groups.Groups.Count > 0)
            {
                defaultGroupId = groups.Groups[0].Name;
                defaultTypeId  = groups.Groups[0].CertificateType;
            }
            else
            {
                return(new NotFoundResult());
            }

            var application = await _opcVault.GetApplicationAsync(id);

            if (application == null)
            {
                return(new NotFoundResult());
            }

            // preset Domain names with discovery Urls
            var domainNames = new List <string>();

            if (application.DiscoveryUrls != null)
            {
                foreach (var discoveryUrl in application.DiscoveryUrls)
                {
                    Uri url = Opc.Ua.Utils.ParseUri(discoveryUrl);
                    if (url == null)
                    {
                        continue;
                    }

                    string domainName = url.DnsSafeHost;
                    if (url.HostNameType != UriHostNameType.Dns)
                    {
                        domainName = Opc.Ua.Utils.NormalizedIPAddress(domainName);
                    }

                    if (!Opc.Ua.Utils.FindStringIgnoreCase(domainNames, domainName))
                    {
                        domainNames.Add(domainName);
                    }
                }
            }

            ViewData["Application"] = application;
            ViewData["Groups"]      = groups;

            var request = new CreateNewKeyPairRequestFormApiModel()
            {
                ApplicationId      = id,
                CertificateGroupId = defaultGroupId,
                CertificateTypeId  = defaultTypeId,
                DomainNames        = domainNames
            };

            UpdateApiModel(request);
            return(View(request));
        }