How to login remotely to jenkins with user credentials?

314 Views Asked by At

currently i'm triggering several jobs with parameters, after a button is clicked on a .net website. The job is then started by an anonymous user.

This is the code for triggering the job:

protected void Btn_Click(object sender, EventArgs e)
    {


        string userName = txtUser.Text;
        string password = txtPassword.Text;
        userName = Base64Util.Base64Encode(userName);
        password = Base64Util.Base64Encode(password);

        string url = Constants.build_jenkins_url + "/job/BUILD1/buildWithParameters?token=authtoken&Checkpoint=" + checkpointInput.Text + "&Git files=false" + "&SHA=None" + "&Build CST=true" + "&CST name=" + CSTNameInput.Text + "&Clean build=true" + "&Run sequences=false&user=" + userName + "&passwd=" + password;
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
        HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
            Response.Redirect(Constants.build_jenkins_url, "_blank", "fullscreen=1,menubar=yes,toolbar=yes,scrollbars=yes,status=yes,titlebar=yes");

        }

I want to trigger the jobs with user credentials. In that case Jenkins will show me, who started the jenkins job. Do you guys have any idea how to achieve this?

2

There are 2 best solutions below

0
On

See information on HttpWebRequest.Credentials.

0
On

Setting basic authentication, it will work:

URL urldemo = new URL(buildUrl);
URLConnection yc = urldemo.openConnection();
String userpass = userName + ":" + password;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());

yc.setRequestProperty ("Authorization", basicAuth);
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));