private bool ValidateUploadedCertFile( HtmlInputFile inputFile, string certificatePassword, out X509Certificate2 cert) { cert = null; if (inputFile.PostedFile == null || inputFile.PostedFile.ContentLength == 0) { this.LabelErrorTestLdapConnection.Text = $"No certificate was passed."; return(false); } // Ensure that fileName is just the file name (no directories), then check that fileName is legal. string fileName = string.Empty; try { fileName = Path.GetFileName(inputFile.PostedFile.FileName); } catch (ArgumentException ex) { this.LabelErrorTestLdapConnection.Text = $"Invalid file path. Error message: {ex.Message}"; return(false); } if (!SPUrlUtility.IsLegalFileName(fileName)) { this.LabelErrorTestLdapConnection.Text = $"The file name is not legal."; return(false); } try { byte[] buffer = new byte[inputFile.PostedFile.ContentLength]; inputFile.PostedFile.InputStream.Read(buffer, 0, buffer.Length); cert = new X509Certificate2(buffer, certificatePassword, X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.Exportable); if (cert.HasPrivateKey == false) { this.LabelErrorTestLdapConnection.Text = $"Certificate does not contain the private key."; return(false); } // Try to export the certificate with its private key to validate that it succeeds cert.Export(X509ContentType.Pkcs12, "Yvan"); } catch (CryptographicException ex) { this.LabelErrorTestLdapConnection.Text = $"Invalid certificate. Error message: {ex.Message}"; return(false); } return(true); }
public static string ConvertToValidUrlFileName(string leafName, char charToReplaceInvalidChar) { if (string.IsNullOrEmpty(leafName)) { throw new ArgumentNullException("leafName"); } if (SPUrlUtility.IsLegalFileName(leafName)) { return(leafName); } StringBuilder stringBuilder = new StringBuilder(); if (charToReplaceInvalidChar != 0 && !SPUrlUtility.IsLegalCharInUrl(charToReplaceInvalidChar)) { charToReplaceInvalidChar = '\0'; } bool flag = charToReplaceInvalidChar != '\0'; string text = leafName.Trim().Trim('.'); char c = '\0'; bool flag2 = false; char[] array = text.ToCharArray(); foreach (char c2 in array) { if (!SPUrlUtility.IsLegalCharInUrl(c2) || (c2 == '.' && c == '.')) { flag2 = true; } else { if (flag2 && flag) { stringBuilder.Append(charToReplaceInvalidChar); flag2 = false; } stringBuilder.Append(c2); c = c2; } } return(stringBuilder.ToString()); }