如何使用特定凭据连接到C#中的TFS服务器?

时间:2010-06-30 14:33:40

标签: c# authentication tfs tfs-sdk

我正在尝试编写一个连接到TFS的c#应用程序并检索工作项信息。不幸的是,似乎所有使用TFS SDK的示例都使用当前用户的默认凭据(即我的域登录信息)。我找到的最接近的信息是使用TeamFoundationServer (String, ICredentials)构造函数,但是我找不到与ICredentials接口接口的合适类的任何信息(特别是因为它似乎没有使用系统。 Net ICredentials,但TeamFoundationServer特定的ICredentials)。

有没有人对使用特定用户名/密码/域名组合登录TFS有任何见解?

3 个答案:

答案 0 :(得分:17)

以下代码可以帮助您:

NetworkCredential cred = new NetworkCredential("Username", "Password", "Domain");
tfs = new TeamFoundationServer("http://tfs:8080/tfs", cred);
tfs.EnsureAuthenticated();

域是实际域,或者在工作组情况下,它将是托管TFS应用程序层的服务器的名称。

答案 1 :(得分:12)

对于TFS 2015& 2017年,提及的对象和方法已被(或正在)弃用。

使用特定凭据连接到TFS:

// For TFS 2015 & 2017

// Ultimately you want a VssCredentials instance so...
NetworkCredential netCred = new NetworkCredential(@"user.name", @"Password1", "DOMAIN");
WindowsCredential winCred = new WindowsCredential(netCred);
VssCredentials vssCred = new VssClientCredentials(winCred);

// Bonus - if you want to remain in control when
// credentials are wrong, set 'CredentialPromptType.DoNotPrompt'.
// This will thrown exception 'TFS30063' (without hanging!).
// Then you can handle accordingly.
vssCred.PromptType = CredentialPromptType.DoNotPrompt;

// Now you can connect to TFS passing Uri and VssCredentials instances as parameters
Uri tfsUri = new Uri(@"http://tfs:8080/tfs");
var tfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri, vssCred);

// Finally, to make sure you are authenticated...
tfsTeamProjectCollection.EnsureAuthenticated();

答案 2 :(得分:3)

多年来,这就是您使用TFS 2013 API的方式:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Example1</title>
    <link rel="stylesheet" href="css/style.css" >
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script>
        function display_case(){
            var num =   document.getElementById('number').value;

                switch(num){

                    case (num = "1"):
                    document.getElementById("result").innerHTML = "You select day Sunday";
                    break;

                    case (num = "2"):
                    document.getElementById("result").innerHTML = "You select day  Monday";
                    break;

                    case (num = "3"):
                    document.getElementById("result").innerHTML = "You select day  Tuesday";
                    break;

                    case (num = "4"):
                    document.getElementById("result").innerHTML = "You select day  Wednesday";
                    break;

                    case (num = "5"):
                    document.getElementById("result").innerHTML = "You select day  Thusday";
                    break;

                    case (num = "6"):
                    document.getElementById("result").innerHTML = "You select day  Friday";
                    break;

                    case (num = "7"):
                    document.getElementById("result").innerHTML = "You select day  Saturday";
                    break;

                    default:
                    document.getElementById("result").innerHTML = "You select day  Invalid Weekday";
                    break
                }

        }
    </script>
</head>
<body>
    <center>
        <div id="error"></div>
        <center>
            <h2> Switch Case Example </h2>
            <p>Enter a Number Between 1 to 7</p>
            <input type="text" id="number" />
            <button onclick="display_case();">Check</button><br />
            <div id="result"><b></b></div>
        </center>
    </center>
</body>

如果这不起作用,请尝试通过其他// Connect to TFS Work Item Store ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain); Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection"); TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, networkCredential); WorkItemStore witStore = new WorkItemStore(tfs); 类传递凭据(为我工作):

Credential