Background
Our company has a limited number of JIRA user licenses, but we want all staff members to access our JIRA dashboard for viewing projects and issues. I’m trying to implement a solution where we use a single read-only JIRA account and create a custom authentication system.
Current Progress
I can successfully authenticate using this curl approach:
curl -v -u "[email protected]:token123" -H "Accept: application/json" -H "Content-Type: application/json" --cookie "session.txt" --cookie-jar "session.txt" -d '{"username": "[email protected]","password": "mypassword"}' -X POST "https://ourcompany.atlassian.net/rest/auth/1/session"
This creates authentication tokens in the session file. I can manually use these tokens in the browser console to log in successfully.
C# Implementation Issue
Here’s my current C# code that isn’t working properly:
Uri dashboardUrl = new Uri("https://ourcompany.atlassian.net/secure/Dashboard.jspa");
// Validate employee credentials against Active Directory
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "ourcompany"))
{
if (context.ValidateCredentials(employeeUser, employeePass))
{
var request = (HttpWebRequest)WebRequest.Create(dashboardUrl);
CookieContainer cookies = new CookieContainer();
cookies.Add(sessionCookie);
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
var credentials = Convert.ToBase64String(Encoding.Default.GetBytes("[email protected]:token123"));
request.Headers.Add("Authorization", "Basic " + credentials);
request.Headers.Add(HttpRequestHeader.Cookie, cookieString);
var payload = "{\"username\":\"[email protected]\",\"password\":\"mypassword\"}";
byte[] requestData = Encoding.Default.GetBytes(payload);
request.ContentLength = requestData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestData, 0, requestData.Length);
requestStream.Flush();
requestStream.Close();
Response.Redirect(jiraInstanceUrl);
}
}
How can I properly handle the session cookies and redirect the user to JIRA while maintaining the authenticated session?