2017-06-17 12 views
3

私はC#Windowsフォームアプリケーションを開発しています。私はJiraに対してユーザーの資格情報をテストしたいと考えています。基本的にユーザーはユーザー名とパスワードを入力し、[OK]をクリックすると、プログラムは資格情報が受け入れられたかどうかを通知します。C#REST APIを使用したJiraに対する資格情報の検証

HttpWebRequestによる基本認証を使用して新しいチケット(別名問題)を作成したり、チケットを閉じたり、ウォッチャーを追加したりする作業コードがすでにあります(これは簡単ですが、私はそれに苦労しています) 。

同様に、System.DirectoryServices.AccountManagement名前空間を使用すると、Active Directoryに対して簡単に資格情報の確認を行うことができます。基本的にはこの方法authenticateAD()は、単にtrueまたはfalseを返します。これは私がのJiraでやりたい事の種類を正確に

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ベースのJira認証のリンクをチェックアウトする場合がありますhttps://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jiraです-rest-api-tutorials/jira-rest-api-example-cookieベースの認証OAuth Jira Authのリンクは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 

       } 
      } 
関連する問題