Skip to content

Commit 6bc9efc

Browse files
author
Justin Marks
committed
2 parents 31e5517 + 711ec0c commit 6bc9efc

File tree

12 files changed

+209
-213
lines changed

12 files changed

+209
-213
lines changed

ClientLibraryConsoleAppSample/ClientLibraryConsoleAppSample.csproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>net452</TargetFramework>
5+
<!--<TargetFramework>netcoreapp2.0</TargetFramework>-->
56
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
67
<RootNamespace>ClientLibraryConsoleAppSample</RootNamespace>
78
<AssemblyName>ClientLibraryConsoleAppSample</AssemblyName>
89
</PropertyGroup>
910
<ItemGroup>
10-
<PackageReference Include="Microsoft.TeamFoundationServer.ExtendedClient" Version="15.118.0-preview" />
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" />
1116
</ItemGroup>
1217
<ItemGroup>
1318
<Reference Include="System" />
1419
<Reference Include="System.Net" />
15-
<Reference Include="System.Net.Http" />
20+
<Reference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net452'" />
1621
<Reference Include="System.Runtime.Serialization" />
17-
<Reference Include="System.ServiceModel" />
22+
<Reference Include="System.ServiceModel" Condition="'$(TargetFramework)' == 'net452'" />
1823
<Reference Include="System.Xml" />
1924
</ItemGroup>
2025
</Project>

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 = "PAT_GOES_HERE"; // 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) and a personal access token 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+
2. Navigate to the sample in cloned repo `vsts-auth-samples/ClientLibraryPatAppSample/`
20+
3. Use [Nuget package restore](https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore) to ensure you have all dependencies installed
21+
4. Open the solution file `ClientLibraryPatAppSample.csproj` in [Visual Studio 2017](https://www.visualstudio.com/downloads/)
22+
5. 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+
6. Build and run solution. After running you should see a list of the IDs all work items which match your query restrictions.

DeviceProfileSample/DeviceProfileSample.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>net452</TargetFramework>
4+
<!--<TargetFramework>net452</TargetFramework>-->
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
56
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
67
<RootNamespace>DeviceProfileSample</RootNamespace>
78
<AssemblyName>DeviceProfileSample</AssemblyName>

ManagedClientConsoleAppSample/App.config

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
<startup>
3+
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
55
</startup>
66
<system.serviceModel>
@@ -33,20 +33,4 @@
3333
<!-- Service Bus specific app setings for messaging connections -->
3434
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]" />
3535
</appSettings>
36-
<runtime>
37-
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
38-
<dependentAssembly>
39-
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
40-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
41-
</dependentAssembly>
42-
<dependentAssembly>
43-
<assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" />
44-
<bindingRedirect oldVersion="0.0.0.0-3.13.8.999" newVersion="3.13.8.999" />
45-
</dependentAssembly>
46-
<dependentAssembly>
47-
<assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory.Platform" publicKeyToken="31bf3856ad364e35" culture="neutral" />
48-
<bindingRedirect oldVersion="0.0.0.0-3.13.8.999" newVersion="3.13.8.999" />
49-
</dependentAssembly>
50-
</assemblyBinding>
51-
</runtime>
5236
</configuration>

ManagedClientConsoleAppSample/ManagedClientConsoleAppSample.csproj

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>net452</TargetFramework>
4+
<TargetFrameworks>net452</TargetFrameworks>
5+
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
56
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
67
<RootNamespace>ManagedClientConsoleAppSample</RootNamespace>
78
<AssemblyName>ManagedClientConsoleAppSample</AssemblyName>
89
</PropertyGroup>
910
<ItemGroup>
10-
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.13.8" />
11-
<PackageReference Include="Microsoft.VisualStudio.Services.InteractiveClient" Version="15.112.1" />
12-
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
11+
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.4" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
12+
<!--<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.19.8" />-->
13+
<PackageReference Include="Microsoft.VisualStudio.Services.InteractiveClient" Version="15.134.0-preview" />
14+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
15+
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
1316
</ItemGroup>
1417
<ItemGroup>
1518
<Reference Include="Microsoft.CSharp" />
1619
<Reference Include="System" />
1720
<Reference Include="System.Core" />
1821
<Reference Include="System.Data" />
19-
<Reference Include="System.Data.DataSetExtensions" />
20-
<Reference Include="System.Net.Http" />
22+
<Reference Include="System.Data.DataSetExtensions" Condition="'$(TargetFramework)' == 'net452'"/>
23+
<Reference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net452'" />
2124
<Reference Include="System.Runtime.Serialization" />
22-
<Reference Include="System.ServiceModel" />
25+
<Reference Include="System.ServiceModel" Condition="'$(TargetFramework)' == 'net452'" />
2326
<Reference Include="System.Xml" />
2427
<Reference Include="System.Xml.Linq" />
2528
</ItemGroup>

ManagedClientConsoleAppSample/Program.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ public static void Main(string[] args)
2222
{
2323
AuthenticationContext ctx = GetAuthenticationContext(null);
2424
AuthenticationResult result = null;
25+
26+
IPlatformParameters promptBehavior = new PlatformParameters();
27+
#if NET452
28+
promptBehavior = new PlatformParameters(PromptBehavior.Always);
29+
#endif
30+
2531
try
2632
{
33+
2734
//PromptBehavior.RefreshSession will enforce an authn prompt every time. NOTE: Auto will take your windows login state if possible
28-
result = ctx.AcquireTokenAsync(VSTSResourceId, clientId, new Uri(replyUri), new PlatformParameters(PromptBehavior.Always)).Result;
35+
result = ctx.AcquireTokenAsync(VSTSResourceId, clientId, new Uri(replyUri), promptBehavior).Result;
2936
Console.WriteLine("Token expires on: " + result.ExpiresOn);
3037

3138
var bearerAuthHeader = new AuthenticationHeaderValue("Bearer", result.AccessToken);
@@ -34,7 +41,7 @@ public static void Main(string[] args)
3441
catch (UnauthorizedAccessException)
3542
{
3643
// If the token has expired, prompt the user with a login prompt
37-
result = ctx.AcquireTokenAsync(VSTSResourceId, clientId, new Uri(replyUri), new PlatformParameters(PromptBehavior.Always)).Result;
44+
result = ctx.AcquireTokenAsync(VSTSResourceId, clientId, new Uri(replyUri), promptBehavior).Result;
3845
}
3946
catch (Exception ex)
4047
{
@@ -67,7 +74,7 @@ private static void ListProjects(AuthenticationHeaderValue authHeader)
6774
{
6875
client.BaseAddress = new Uri(vstsCollectionUrl);
6976
client.DefaultRequestHeaders.Accept.Clear();
70-
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
77+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7178
client.DefaultRequestHeaders.Add("User-Agent", "ManagedClientConsoleAppSample");
7279
client.DefaultRequestHeaders.Add("X-TFS-FedAuthRedirect", "Suppress");
7380
client.DefaultRequestHeaders.Authorization = authHeader;
@@ -79,7 +86,8 @@ private static void ListProjects(AuthenticationHeaderValue authHeader)
7986
if (response.IsSuccessStatusCode)
8087
{
8188
Console.WriteLine("\tSuccesful REST call");
82-
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
89+
var result = response.Content.ReadAsStringAsync().Result;
90+
Console.WriteLine(result);
8391
}
8492
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
8593
{

0 commit comments

Comments
 (0)