public IActionResult Create([FromBody] CreateServerModel model) { LCServer server = _mapper.Map <LCServer>(model); _serverService.Create(server, long.Parse(User.Identity.Name)); return(Ok()); }
public ActionResult createServer([FromBody] CreateServerModel objparams) { AzureRestCaller restCall = new AzureRestCaller(); var model = restCall.CreateServer(objparams); return(PartialView("_NewServrList", model)); }
public AzureCreateServerViewModel CreateServer(CreateServerModel objParams) { AzureCreateServerViewModel objCreateServerModel = new AzureCreateServerViewModel(); //First read the certificate from certificate store which will be used to authenticate the request. try { X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); certificateStore.Open(OpenFlags.ReadOnly); X509Certificate2Collection certificates = certificateStore.Certificates; certificateThumbprint = Regex.Replace(certificateThumbprint, @"[^\u0000-\u007F]", string.Empty); // to remove whitespace. var matchingCertificates = certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false); var managementCert = matchingCertificates[0]; X509Certificate2 cert = new X509Certificate2(certificateFile); string requestUrl = string.Format(CultureInfo.InvariantCulture, "https://management.database.windows.net:8443/{0}/servers", objParams.subscriptionid); HttpWebRequest webRequest = HttpWebRequest.Create(requestUrl) as HttpWebRequest; webRequest.ClientCertificates.Add(managementCert); webRequest.Headers["x-ms-version"] = "1.0"; webRequest.Method = "POST"; //=== Add the Request Payload ===// string xmlBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<Server xmlns=\"http://schemas.microsoft.com/sqlazure/2010/12/\">\n" + " <AdministratorLogin>" + objParams.AdminLogin + "</AdministratorLogin>\n" + " <AdministratorLoginPassword>" + objParams.AdminLoginPassword + "</AdministratorLoginPassword>\n" + " <Location>" + objParams.DataCenterLocation + "</Location>\n" + "</Server>"; byte[] bytes = Encoding.UTF8.GetBytes(xmlBody); webRequest.ContentLength = bytes.Length; webRequest.ContentType = "application/xml;charset=utf-8"; using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } //=== Send the request and wait on the response ===// using (WebResponse webResponse = webRequest.GetResponse()) { //=== x-ms-request-id is useful for troubleshooting request failures with Microsoft ===// objCreateServerModel.Requestid = webResponse.Headers["x-ms-request-id"]; objCreateServerModel.returnStatusCode = (int)((HttpWebResponse)webResponse).StatusCode; objCreateServerModel.AdminName = objParams.AdminLogin; objCreateServerModel.DataCenterLocation = objParams.DataCenterLocation; using (Stream stream = webResponse.GetResponseStream()) { using (XmlTextReader xmlReader = new XmlTextReader(stream)) { while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: if (xmlReader.Name == "ServerName") { if (xmlReader.Read() && (xmlReader.NodeType == XmlNodeType.Text)) { objCreateServerModel.NewServerName = xmlReader.Value.ToString(); } } break; } } } } } } catch (WebException webEx) { var resp = webEx.Response; objCreateServerModel = new AzureCreateServerViewModel(); objCreateServerModel.Requestid = resp.Headers["x-ms-request-id"]; objCreateServerModel.returnStatusCode = (int)((HttpWebResponse)resp).StatusCode; objCreateServerModel.ErrorMsg = webEx.Message; } return(objCreateServerModel); }