I’m trying to fetch Jira issues using C# but keep getting a 401 Unauthorized error. Here’s my code:
public ActionResult FetchIssuesFromJira(string userLogin, string userPassword, string baseUrl)
{
string apiEndpoint = baseUrl + "/rest/api/2/search";
var httpClient = new HttpClient();
var authBytes = Encoding.ASCII.GetBytes(userLogin + ":" + userPassword);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authBytes));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage result = httpClient.GetAsync(apiEndpoint).Result;
if (result.IsSuccessStatusCode)
{
// process result
}
}
The weird thing is that when I test the same URL in my browser, it works fine and returns JSON data. My credentials are definitely correct too. Any idea what might be causing this authentication issue in the C# code?
Add a user-agent header to your httpclient request. Some JIRA instances block requests without proper user-agent headers even when auth’s correct. Also check your baseURL doesn’t have a trailing slash - I’ve seen that cause weird auth failures. Worth testing with Postman first to see if it’s a C# issue or server-side problem.
Check if your Jira server has CAPTCHA enabled. After too many failed logins, Jira kicks in CAPTCHA protection that blocks API calls even with the right credentials. Your browser probably already solved the CAPTCHA - that’s why it works there but not in code. Log into Jira’s web interface first to clear any failed attempts, then try your API call again. Also check for URL encoding problems. If your password has special characters like @, #, or &, they need proper encoding. I’ve seen passwords work fine in browsers (they auto-encode) but fail in HttpClient without manual encoding.
Had the exact same issue last month with our Jira integration. Your Jira instance probably needs API tokens instead of regular passwords for REST API auth. Browser login works fine with your password, but most Jira setups disable basic auth with passwords for security. Go to your Atlassian account settings and generate an API token. Then use your email as username and the token as password in your code. Also check if your Jira admin enabled basic auth for REST APIs - some orgs disable it completely and force OAuth instead.