可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've written below C# code to login to JIRA Rest API:
var url = new Uri("http://localhost:8090/rest/auth/latest/session?os_username=tempusername&os_password=temppwd"); var request = WebRequest.Create(url) as HttpWebRequest; if (null == request) { return ""; } request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = 200; request.KeepAlive = false; using (var response = request.GetResponse() as HttpWebResponse) { }
When I execute this, application just goes on running without returning any response. Please suggest if this is the right way of calling JIRA Login using REST API
回答1:
For basic authentication you need to send in the username and password in a base64-encoding. Guidelines can be found in the API examples on atlassians developer page: https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Basic+Authentication , if you are doing it in C# you need to send the encoded data in the header in the following format:
"Authorization: Basic [ENCODED CREDENTIALS]"
Here is a simple example:
public enum JiraResource { project } protected string RunQuery( JiraResource resource, string argument = null, string data = null, string method = "GET") { string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString()); if (argument != null) { url = string.Format("{0}{1}/", url, argument); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ContentType = "application/json"; request.Method = method; if (data != null) { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(data); } } string base64Credentials = GetEncodedCredentials(); request.Headers.Add("Authorization", "Basic " + base64Credentials); HttpWebResponse response = request.GetResponse() as HttpWebResponse; string result = string.Empty; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } return result; } private string GetEncodedCredentials() { string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password); byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials); return Convert.ToBase64String(byteCredentials); }
(JiraResource is just an enum I use to decide which part of the API to use)
I hope this will help!
回答2:
Here is a simpler solution which works as required:
var mergedCredentials = string.Format("{0}:{1}", username, password); var byteCredentials = Encoding.UTF8.GetBytes(mergedCredentials); var encodedCredentials = Convert.ToBase64String(byteCredentials); using (WebClient webClient = new WebClient()) { webClient.Headers.Set("Authorization", "Basic " + encodedCredentials); return webClient.DownloadString(url); }
回答3:
If you don't want to encode your credentials in every request here is how to do it using cookies.
When requesting the cookie you don't need to add any authorization on the headers. This method will accept a JSON string with the user name and password and the URL. It will return the cookie values.
public async Task GetCookieAsync(string myJsonUserNamePassword, string JiraCookieEndpointUrl) { using (var client = new HttpClient()) { var response = await client.PostAsync( JiraCookieEndpointUrl, new StringContent(myJsonUserNamePassword, Encoding.UTF8, "application/json")); var json = response.Content.ReadAsStringAsync().Result; var jiraCookie= JsonConvert.DeserializeObject(json); return jArr; } } public class JiraCookie { public Session session { get; set; } } public class Session { public string name { get; set; } public string value { get; set; } }
When I call it using url: http://[baseJiraUrl]/rest/auth/1/session it returns the following JSON response:
{ "session" : -{ "name" : JSESSIONID, "value" : cookieValue }
Keep in mind the URL above is valid in the version of JIRA I'm using and may vary depending on which version you're using. Read the JIRA API documentation for the correct URL for the version you are using. I'm using the following: https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#auth/1/session
Remember you'll have to store your cookie and use it on every subsequent request. Check out this answer on how add cookies to your HttpClient request: How do I set a cookie on HttpClient's HttpRequestMessage.
Once you're done with the cookie (logging out) simply send a delete http request with the same URL as the post.