I have a service which import tasks from TFS and Azure Devops. I use Microsoft.TeamFoundationServer.Client 16.153.0
I'm trying to connect to TFS using next code
var httpClient = new WorkItemTrackingHttpClient(new Uri(_settings.ServerAddress), new VssBasicCredential(_settings.Login, _settings.Password));
var taskQuery = "..."
var queryResult = await httpClient.QueryByWiqlAsync(tasksQuery, timePrecision:true);
This code works only for first time. If I change login/password and import tasks again it still using previous login/password even if it wrongs. And It doesn't work for azure devops.
What do I wrong?
Try using the following code to connect to DevOps. It obtain the PAT you defined in the code:
Uri uri = new Uri(_uri);
string personalAccessToken = _personalAccessToken;
string project = _project;
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
Related
Trying to access discovery client for acceising other endpoints anf following with,
http://docs.identityserver.io/en/aspnetcore1/endpoints/discovery.html
Installed IdentityModel nuget package in .Net 7.5 MVC application. But unable to find the DiscoveryClient.
var discoveryClient = new DiscoveryClient("https://demo.identityserver.io");
var doc = await discoveryClient.GetAsync();
Is there something change in Identitymodel for IdentityServer4
Also, unable to find parameter for "Tokenclient".
Able to figure out, change in IdentityModel, its all extension of HttpClient.
https://identitymodel.readthedocs.io/en/latest/client/discovery.html
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://demo.identityserver.io");
Yes, you are correct. There are lot of changes in the IdentityModel NuGet package.
Below code will help you:
HttpClient httpClient = new HttpClient();
//Below code will give you discovery document response previously we were creating using DiscoveryClient()
// They have created `.GetDiscoveryDocumentAsync()` extension method to get discovery document.
DiscoveryDocumentResponse discoveryDocument = await httpClient.GetDiscoveryDocumentAsync();
// To create a token you can use one of the following methods, which totally depends upon which grant type you are using for token generation.
Task<TokenResponse> RequestAuthorizationCodeTokenAsync(AuthorizationCodeTokenRequest)
Task<TokenResponse> RequestClientCredentialsTokenAsync(ClientCredentialsTokenRequest)
Task<TokenResponse> RequestDeviceTokenAsync(DeviceTokenRequest)
Task<TokenResponse> RequestPasswordTokenAsync(PasswordTokenRequest)
Task<TokenResponse> RequestRefreshTokenAsync(RefreshTokenRequest)
Task<TokenResponse> RequestTokenAsync(TokenRequest)
For example if you want to create a token for password grant type then use below code:
PasswordTokenRequest passwordTokenRequest = new PasswordTokenRequest()
{
Address = discoveryDocument.TokenEndpoint,
ClientId = ClientName,
ClientSecret = ClientSecret,
GrantType = GrantTypes.ResourceOwnerPassword,
Scope = scope,
UserName = userName,
Password = password
};
httpClient.RequestPasswordTokenAsync(passwordTokenRequest);
I hope this will help you!
If you used some sample code and the other answers aren't working, because HttpClient doesn't have GetDiscoveryDocumentAsync
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://localhost:5001");
Update your IdentityModel package, in Visual Studio:
Right click Dependencies -> Manage Nuget Packages -> Updates (select "All" in top right corner)
Hello trying to connect with my username/password that I use in VS or when logging on web site - but I get this error: VssUnauthorizedException: 'VS30063: You are not authorized to access https://dev.azure.com.'
Or do I have to use a rest oauth token?
var serverUrl = new Uri("https://dev.azure.com/mysite/");
var clientCredentials = new VssBasicCredential(username, password);
var connection = new VssConnection(serverUrl, clientCredentials);
var buildServer = connection.GetClient<BuildHttpClient>();
var sourceControlServer = connection.GetClient<TfvcHttpClient>();
var changesets = buildServer.GetChangesBetweenBuildsAsync("My Project", 1, 1000).Result;
Please try the following code:
var u = new Uri("https://dev.azure.com/mysite");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("username", "password")));
var connection = new VssConnection(u, c);
As I see you are trying to connect to the azure devops service because your url is https://dev.azure.com/mysite. For TFS we use this template: http://server_name:8080/tfs.
You can not use a user name and password for azure devops service. Use a personal access token and this code:
VssConnection connection = new VssConnection(new Uri("your_url"), new VssBasicCredential(string.Empty, "your_pat"));
Additional information: Authenticating (Azure DevOps Services)
We are moving to the web style builds on TFS 2015 (Update 4).
I've always been able to retrieve information about builds using the code below, but that is not retrieving our new builds created via the web interface.
Is there a reasonable way to modify my code to bring in both legacy builds and the new style builds?
If not, I assume it is time for me to figure out how to use the REST API. Any tips for an equivalent query would be appreciated.
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://SERVERINFO"));
IBuildServer buildServer = (IBuildServer) tfs.GetService(typeof (IBuildServer));
var buildDetail = buildServer.CreateBuildDetailSpec("*");
buildDetail.MinFinishTime = DateTime.Now.Date.AddDays(-1);
buildDetail.InformationTypes = null;
buildDetail.QueryDeletedOption = QueryDeletedOption.IncludeDeleted;
buildDetail.MaxBuildsPerDefinition = 1; //Only return the most recent of each build type...or comment out to return all builds with this definition
var builds = buildServer.QueryBuilds(buildDetail).Builds.Select(....
The old XAML build system uses a SOAP API.The task-based new vNet build system does not have a SOAP API. It's using REST API. I'm afraid you could not just modify the code to get new builds. They do not support Build vNext as they were written before their time.
Besides the SOAP API is slowly being replaced with a REST API, especially in some new features.Since you are moving to vNext build on TFS2015 update4. Highly recommend you stat to use Rest API.
You can access it from C# code either by querying the REST API directly or by using the Team Foundation Server Client NuGet package. A Sample:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.WebApi;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Uri tfsurl = new Uri("http://xxxx:8080/tfs/CollectionName");
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(tfsurl);
BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>();
List<Build> builds = bhc.GetBuildsAsync("ProjectName").Result;
foreach (Build bu in builds)
{
Console.WriteLine(bu.BuildNumber);
}
Console.ReadLine();
}
}
}
By using the Rest API in the libraries as above, you could be able to get both XAML and vNext builds.
I am trying to fetch Build Warning from MS Build,(in Build which contain or having number of solutions)
Is it possible to fetch using TFS API, or any TFS DB using QUERY ?
You could use this TFS REST API to get logs of a TFS builds. To get those logs out, you need to fetch those warnings by yourself. There's no API to only get warnings.
Http method: GET
http:/servername"8080/tfs/DefaultCollection/teamproject/_apis/build/builds/391/logs?api-version=2.0
You could also install a TFS ExtendedClient Nuget package to use TFS object model API.
Here is the code snippet:
Like the comment said above, the VNext build definition information couldn't be reached using the old version API. Install this TFS ExtendedClient Nuget package for your project, using the method below to get all build definitions.
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Operations;
private static void GetBuildWarnings()
{
var u = new Uri("http://v-tinmo-12r2:8080/tfs/MyCollection/");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("username", "password", "domain")));
var connection = new VssConnection(u, c);
BuildHttpClient buildServer = connection.GetClient<BuildHttpClient>();
List<BuildLog> logs = buildServer.GetBuildLogsAsync("teamprojectname",buildId).Result;
foreach (BuildLog log in logs)
{
var list = buildServer.GetBuildLogLinesAsync("A92FB795-A956-45B5-A017-7A7DFB96A040",buildId,log.Id).Result; //A92FB795-A956-45B5-A017-7A7DFB96A040 is the team project Guid
foreach (var line in list)
{
if (l.Contains("[Warning]"))
{
Console.WriteLine(line);
}
}
}
Console.ReadLine();
}
I developed a simple app to do some custom things for our company view the TFS SDK. I was able to get this working for myself locally, but when I deploy the site to a web server I get a not authorized error:
TF30063: You are not authorized to access xxx
This only works locally for me if I am signed into the Team Project from Visual Studio 2012 and running locally from there. Anyone have thoughts on why I can't get this to work when I deploy to another server? Here is the code... Fails at EnsureAuthenticated() (I have also tried the 'Authenticated()' method).
var netCred = new NetworkCredential("username", "password", "domain");
var basCred = new BasicAuthCredential(netCred);
var tfsCred = new TfsClientCredentials(basCred);
tfsCred.AllowInteractive = false;
tfs = new TfsConfigurationServer(new Uri("https://tfsurl"), tfsCred);
tfs.EnsureAuthenticated();
If you are connecting to a TFS server on your domain then you can use NetworkCredential to authenticate:
Uri uri = new Uri("http://vsalm:8080/tfs");
ICredentials credentials = new NetworkCredential("User", "Password");
using (TfsConfigurationServer server = new TfsConfigurationServer(uri, credentials))
{
server.EnsureAuthenticated();
}
If you are connecting to the hosted Team Foundation Service then you may need to enable alternate credentials.