我的应用程序连接到Jira进行数据提取。为此,我使用了以下代码:
public string RunQuery(JiraRessource resource, string project_id, int startAt, int maxResults, string method = "GET")
{
string url = string.Format(m_BaseUrl);
if (project_id != null)
{
string jql = "search?jql=project=" + project_id;
url = string.Format("{0}{1}", url, jql);
}
string jqr = "&startAt=" + startAt + "&maxResults=" + maxResults;
url = string.Format("{0}{1}", url, jqr);
Console.WriteLine(url);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
string base64Credentials = GetEncodedCredentials();
//string base64Credentials = WindowsIdentity.GetCurrent().Token.ToString();
request.Headers.Add("Authorization", "Basic " + base64Credentials);
string result = string.Empty;
using (WebResponse response = request.GetResponse())
{
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);
}
用户必须输入用户名和密码才能连接。我想更改它以便使用Windows凭据直接连接。我不知道该怎么做。可能吗?如果是的话,我该怎么办?
答案 0 :(得分:0)
WebRequest类具有Credentials属性。您可以使用新的NetworkCredential对象进行设置。您也可以将其设置为登录Windows用户。示例代码;
request.Credentials = new NetworkCredential(user, pwd, domain);
或
request.Credentials = CredentialCache.DefaultNetworkCredentials;