/// <summary> /// Logs in and returns the session ID /// </summary> /// <param name="user"></param> /// <param name="password"></param> /// <returns></returns> private PkildSession Login_Internal(String user, SecureString password) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BaseUri); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Headers[HttpRequestHeader.Cookie] = "pkild_session=" + Session.SessionID; using (Stream reqStream = request.GetRequestStream()) using (StreamWriter writer = new StreamWriter(reqStream)) { String requestData = String.Format( "username={0}&password={1}&login=Submit", HttpUtility.UrlEncode(user, Encoding.UTF8), HttpUtility.UrlEncode(password.ConvertToUnsecureString(), Encoding.UTF8)); writer.Write(requestData); requestData.Zero(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception("Failed to log in: Server returned status code " + response.StatusCode.ToString()); String cookies = response.Headers[HttpResponseHeader.SetCookie]; using (Stream responseStream = response.GetResponseStream()) { var parser = new ResponseParser(new StreamReader(responseStream).ReadToEnd()); certRevokeNode = parser.RevocationNodeName; CertificateState = parser.CertificateState; } if (cookies.ToString().StartsWith("pkild_session")) return new PkildSession(cookies.ToString()); throw new Exception("Unexpected response from server"); } }
private void FetchCertificateState() { CertificateState = net.CertificateState.Unknown; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(BaseUri.ToString() + "/action/select/6e65775f63657274")); request.Method = "POST"; request.Headers[HttpRequestHeader.Cookie] = "pkild_session=" + Session.SessionID; request.Headers["X-Requested-With"] = "XMLHttpRequest"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception("Unexpected status code: " + response.StatusCode); using (Stream respStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(respStream)) { var parser = new ResponseParser(reader.ReadToEnd()); CertificateState = parser.CertificateState; certRevokeNode = parser.RevocationNodeName; } } }
public bool RevokeCertificate() { if (CertificateState != CertificateState.Present) throw new Exception("Can't revoke a missinr or already revoked certificate"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BaseUri); request.Method = "POST"; String requestData = "revoke_cert=revoke&action_type=revoke_cert&node_name=" + certRevokeNode; request.ContentType = "application/x-www-form-urlencoded"; request.Headers[HttpRequestHeader.Cookie] = "pkild_session=" + Session.SessionID; using (Stream reqStream = request.GetRequestStream()) using (StreamWriter writer = new StreamWriter(reqStream)) { writer.Write(requestData); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception("Failed to revoke certificate: Server returned status code " + response.StatusCode.ToString()); using (Stream responseStream = response.GetResponseStream()) { var parser = new ResponseParser(new StreamReader(responseStream).ReadToEnd()); certRevokeNode = parser.RevocationNodeName; } } FetchCertificateState(); return CertificateState == CertificateState.Revoked; }