2017-06-17 88 views
3

我正在開發一個C#Windows窗體應用程序,我希望能夠測試用戶對Jira的憑據。基本上用戶會輸入他們的用戶名和密碼,點擊確定,程序會告訴他們他們的憑證是否被接受。C#使用REST API對Jira進行憑據驗證

我已經有工作代碼(見下文),通過HttpWebRequest使用基本身份驗證來創建新的票證(又名問題),關閉票據,添加監視器等 - 所以我覺得這很容易,但我努力與它。

作爲一種模擬,您可以使用System.DirectoryServices.AccountManagement命名空間非常輕鬆地對Active Directory執行憑據檢查。基本上,該方法authenticateAD()只會返回true或false:

private bool authenticateAD(string username, string password) 
{ 
    PrincipalContext pc = new PrincipalContext(ContextType.Domain, "example.com"); 
    bool isValid = pc.ValidateCredentials(username,password); 
    return isValid; 
} 

這正是我想與吉拉做的事情的那種。

僅供參考,以下是我用於在jira中添加/關閉/更新門票的代碼 - 也許可以修改它以執行我想要的操作?

private Dictionary<string, string> sendHTTPtoREST(string json, string restURL) 
{ 
    HttpWebRequest request = WebRequest.Create(restURL) as HttpWebRequest; 
    request.Method = "POST"; 
    request.Accept = "application/json"; 
    request.ContentType = "application/json"; 
    string mergedCreds = string.Format("{0}:{1}", username, password); 
    byte[] byteCreds = UTF8Encoding.UTF8.GetBytes(mergedCreds); 
    request.Headers.Add("Authorization", "Basic " + byteCreds); 
    byte[] data = Encoding.UTF8.GetBytes(json); 
    try 
    { 
     using (var requestStream = request.GetRequestStream()) 
     { 
      requestStream.Write(data, 0, data.Length); 
      requestStream.Close(); 
     } 
    } 
    catch(Exception ex) 
    { 
     displayMessages(string.Format("Error creating Jira: {0}",ex.Message.ToString()), "red", "white"); 
     Dictionary<string, string> excepHTTP = new Dictionary<string, string>(); 
     excepHTTP.Add("error", ex.Message.ToString()); 
     return excepHTTP; 
    } 
    response = (HttpWebResponse)request.GetResponse(); 
    var reader = new StreamReader(response.GetResponseStream()); 
    string str = reader.ReadToEnd(); 
    var jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 
    var sData = jss.Deserialize<Dictionary<string, string>>(str); 

    if(response.StatusCode.ToString()=="NoContent") 
    { 
     sData.Add("code", "NoContent"); 
     request.Abort(); 
     return sData; 
    } 
    else 
    { 
     sData.Add("code", response.StatusCode.ToString()); 
     request.Abort(); 
     return sData; 
    } 
} 

謝謝!

+0

你可能想看看基於Cookie的吉拉驗證鏈接https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira -rest-api-tutorials/jira-rest-api-example-cookie-based-authentication。 OAuth Jira身份驗證鏈接是https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-oauth-authentication – sBanda

回答

1

如何嘗試訪問JIRA的根頁面並查看您是否收到HTTP 403錯誤?

 try 
     { 
      // access JIRA using (parts of) your existing code 
     } 
     catch (WebException we) 
     { 
      var response = we.Response as HttpWebResponse; 
      if (response != null && response.StatusCode == HttpStatusCode.Forbidden) 
      { 
       // JIRA doesn't like your credentials 
      } 
     } 
0

HttpClient將是簡單和最好使用與GetAsync檢查憑據。

的示例代碼如下

using (HttpClient client = new HttpClient()) 
      { 
       client.BaseAddress = new Uri(JiraPath); 
       // Add an Accept header for JSON format. 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
string mergedCreds = string.Format("{0}:{1}", username, password); 
byte[] byteCreds = UTF8Encoding.UTF8.GetBytes(mergedCreds); 
       var authHeader = new AuthenticationHeaderValue("Basic", byteCreds); 
       client.DefaultRequestHeaders.Authorization = authHeader; 
       HttpResponseMessage response = client.GetAsync(restURL).Result; // Blocking call! 
       if (response.IsSuccessStatusCode) 
       { 
        strJSON = response.Content.ReadAsStringAsync().Result; 
        if (!string.IsNullOrEmpty(strJSON)) 
         return strJSON; 
       } 
       else 
       { 
        exceptionOccured = true; 
        // Use "response.ReasonPhrase" to return error message 

       } 
      } 
相關問題