Skip to content

Commit f2eb97e

Browse files
committed
Sample using a PAT token to read work items
1 parent a727b93 commit f2eb97e

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

ClientLibraryPatAppSample/App.config

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
14+
<system.serviceModel>
15+
<extensions>
16+
<!-- In this extension section we are introducing all known service bus extensions. User can remove the ones they don't need. -->
17+
<behaviorExtensions>
18+
<add name="connectionStatusBehavior" type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
19+
<add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
20+
<add name="serviceRegistrySettings" type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
21+
</behaviorExtensions>
22+
<bindingElementExtensions>
23+
<add name="netMessagingTransport" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
24+
<add name="tcpRelayTransport" type="Microsoft.ServiceBus.Configuration.TcpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
25+
<add name="httpRelayTransport" type="Microsoft.ServiceBus.Configuration.HttpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
26+
<add name="httpsRelayTransport" type="Microsoft.ServiceBus.Configuration.HttpsRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
27+
<add name="onewayRelayTransport" type="Microsoft.ServiceBus.Configuration.RelayedOnewayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
28+
</bindingElementExtensions>
29+
<bindingExtensions>
30+
<add name="basicHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
31+
<add name="webHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
32+
<add name="ws2007HttpRelayBinding" type="Microsoft.ServiceBus.Configuration.WS2007HttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
33+
<add name="netTcpRelayBinding" type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
34+
<add name="netOnewayRelayBinding" type="Microsoft.ServiceBus.Configuration.NetOnewayRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
35+
<add name="netEventRelayBinding" type="Microsoft.ServiceBus.Configuration.NetEventRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
36+
<add name="netMessagingBinding" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
37+
</bindingExtensions>
38+
</extensions>
39+
</system.serviceModel>
40+
<appSettings>
41+
<!-- Service Bus specific app setings for messaging connections -->
42+
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]" />
43+
</appSettings>
44+
</configuration>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net452</TargetFramework>
5+
<!--<TargetFramework>netcoreapp2.0</TargetFramework>-->
6+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
7+
<RootNamespace>ClientLibraryConsoleAppSample</RootNamespace>
8+
<AssemblyName>ClientLibraryConsoleAppSample</AssemblyName>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.4" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
12+
<PackageReference Include="Microsoft.TeamFoundationServer.ExtendedClient" Version="16.139.0-preview" />
13+
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
14+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
15+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<Reference Include="System" />
19+
<Reference Include="System.Net" />
20+
<Reference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net452'" />
21+
<Reference Include="System.Runtime.Serialization" />
22+
<Reference Include="System.ServiceModel" Condition="'$(TargetFramework)' == 'net452'" />
23+
<Reference Include="System.Xml" />
24+
</ItemGroup>
25+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26228.9
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientLibraryPatAppSample", "ClientLibraryPatAppSample.csproj", "{F7AC99CF-0972-4727-AC95-F537EF4EB4EA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F7AC99CF-0972-4727-AC95-F537EF4EB4EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F7AC99CF-0972-4727-AC95-F537EF4EB4EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F7AC99CF-0972-4727-AC95-F537EF4EB4EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F7AC99CF-0972-4727-AC95-F537EF4EB4EA}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

ClientLibraryPatAppSample/Program.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Linq;
3+
4+
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
5+
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
6+
using Microsoft.VisualStudio.Services.Common;
7+
using Microsoft.VisualStudio.Services.WebApi;
8+
9+
namespace ClientLibraryConsoleAppSample
10+
{
11+
class Program
12+
{
13+
//============= Config [Edit these with your settings] =====================
14+
internal const string vstsCollectionUrl = "https://myaccount.visualstudio.com"; //change to the URL of your VSTS account; NOTE: This must use HTTPS
15+
// internal const string vstsCollectioUrl = "http://myserver:8080/tfs/DefaultCollection" alternate URL for a TFS collection
16+
internal const string pat = ""; // change to the generated value
17+
//==========================================================================
18+
19+
//Console application to execute a user defined work item query
20+
static void Main(string[] args)
21+
{
22+
//Prompt user for credential
23+
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssBasicCredential(string.Empty, pat));
24+
25+
//create http client and query for resutls
26+
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
27+
Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Bug' AND [Assigned To] = @Me" };
28+
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;
29+
30+
//Display reults in console
31+
if (queryResults == null || queryResults.WorkItems.Count() == 0)
32+
{
33+
Console.WriteLine("Query did not find any results");
34+
}
35+
else
36+
{
37+
foreach (var item in queryResults.WorkItems)
38+
{
39+
Console.WriteLine(item.Id);
40+
}
41+
}
42+
}
43+
}
44+
}

ClientLibraryPatAppSample/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Client Libraries Sample
2+
3+
For native applications the best way to authenticate and access VSTS resources is using the [Client Libraries](https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/dotnet). They are .NET libraries made to simplify integration with Visual Studio Team Services and Team Foundation Server (2015 and later). They allow access to both the Traditional Client Object Model and [new REST APIs](https://www.visualstudio.com/en-us/docs/integrate/api/overview).
4+
5+
## Sample Application
6+
7+
This buildable sample will walk you through the steps to create an application which uses the [Client Libraries](https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/dotnet) to open an interactive login prompt and use that authentication state to execute a user pre-defined query written in [Work Item Query Language](https://msdn.microsoft.com/en-us/library/bb130198(v=vs.90).aspx). Query results are output into the console.
8+
9+
## Step 1: Clone or download vsts-auth-samples repository
10+
11+
From a shell or command line:
12+
```no-highlight
13+
git clone https://github.com/Microsoft/vsts-auth-samples.git
14+
```
15+
16+
## Step 2: Run the sample
17+
18+
1. Generate a [PAT token](https://docs.microsoft.com/en-us/vsts/organizations/accounts/use-personal-access-tokens-to-authenticate?view=vsts) and grant it "Work items (read)" permission and make a copy of the generated value
19+
1. Navigate to the sample in cloned repo `vsts-auth-samples/ClientLibraryPatAppSample/`
20+
2. Use [Nuget package restore](https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore) to ensure you have all dependencies installed
21+
3. Open the solution file `ClientLibraryPatAppSample.csproj` in [Visual Studio 2017](https://www.visualstudio.com/downloads/)
22+
4. Open CS file `Program.cs` and there is a section with input values to change at the top of the class:
23+
* `vstsCollectionUrl` - Mutable value. This is the url to your VSTS/TFS collection, e.g. http://myaccount.visualstudio.com for VSTS or http://myserver:8080/tfs/DefaultCollection for TFS.
24+
* `pat` - Mutable value. This is the value you generated in step 1.
25+
5. Build and run solution. After running you should see a list of the IDs all work items which match your query restrictions.

0 commit comments

Comments
 (0)