Skip to content

Commit 56380d7

Browse files
authored
Samples upgraded to MSAL (#105)
* Updated ClientLibraryConsoleAppSample * Updated ClientLibraryPatAppSample * Updated DeviceProfileSample * Updated DualSupportClientSample * Updated ManagedClientConsoleAppSample * Updated NonInteractivePatGenerationSample * Added missing scripts and stylesheets * Added a new sample based on blog post request * Resolved PR comments
1 parent 30ad4e1 commit 56380d7

File tree

69 files changed

+29929
-15945
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+29929
-15945
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33213.308
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountProviderSample", "AccountProviderSample\AccountProviderSample.csproj", "{3EE4CED9-4158-468C-9F88-1035CA95A92C}"
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+
{3EE4CED9-4158-468C-9F88-1035CA95A92C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3EE4CED9-4158-468C-9F88-1035CA95A92C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3EE4CED9-4158-468C-9F88-1035CA95A92C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3EE4CED9-4158-468C-9F88-1035CA95A92C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {44BC107C-DEA4-475C-A440-A57F48F818E1}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3EE4CED9-4158-468C-9F88-1035CA95A92C}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>AccountProviderSample</RootNamespace>
10+
<AssemblyName>AccountProviderSample</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Microsoft.Identity.Client, Version=4.46.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Microsoft.Identity.Client.4.46.1\lib\net461\Microsoft.Identity.Client.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Microsoft.Identity.Client.Extensions.Msal, Version=2.23.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
40+
<HintPath>..\packages\Microsoft.Identity.Client.Extensions.Msal.2.23.0\lib\net45\Microsoft.Identity.Client.Extensions.Msal.dll</HintPath>
41+
</Reference>
42+
<Reference Include="Microsoft.IdentityModel.Abstractions, Version=6.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Microsoft.IdentityModel.Abstractions.6.22.0\lib\net472\Microsoft.IdentityModel.Abstractions.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Microsoft.IdentityModel.JsonWebTokens, Version=6.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46+
<HintPath>..\packages\Microsoft.IdentityModel.JsonWebTokens.6.22.0\lib\net472\Microsoft.IdentityModel.JsonWebTokens.dll</HintPath>
47+
</Reference>
48+
<Reference Include="Microsoft.IdentityModel.Logging, Version=6.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Microsoft.IdentityModel.Logging.6.22.0\lib\net472\Microsoft.IdentityModel.Logging.dll</HintPath>
50+
</Reference>
51+
<Reference Include="Microsoft.IdentityModel.Tokens, Version=6.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
52+
<HintPath>..\packages\Microsoft.IdentityModel.Tokens.6.22.0\lib\net472\Microsoft.IdentityModel.Tokens.dll</HintPath>
53+
</Reference>
54+
<Reference Include="Microsoft.TeamFoundation.Common, Version=19.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
55+
<HintPath>..\packages\Microsoft.VisualStudio.Services.Client.19.212.0-preview\lib\net462\Microsoft.TeamFoundation.Common.dll</HintPath>
56+
</Reference>
57+
<Reference Include="Microsoft.VisualStudio.Services.Client.Interactive, Version=19.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
58+
<HintPath>..\packages\Microsoft.VisualStudio.Services.InteractiveClient.19.212.0-preview\lib\net462\Microsoft.VisualStudio.Services.Client.Interactive.dll</HintPath>
59+
</Reference>
60+
<Reference Include="Microsoft.VisualStudio.Services.Common, Version=19.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Microsoft.VisualStudio.Services.Client.19.212.0-preview\lib\net462\Microsoft.VisualStudio.Services.Common.dll</HintPath>
62+
</Reference>
63+
<Reference Include="Microsoft.VisualStudio.Services.WebApi, Version=19.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
64+
<HintPath>..\packages\Microsoft.VisualStudio.Services.Client.19.212.0-preview\lib\net462\Microsoft.VisualStudio.Services.WebApi.dll</HintPath>
65+
</Reference>
66+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
67+
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
68+
</Reference>
69+
<Reference Include="System" />
70+
<Reference Include="System.Core" />
71+
<Reference Include="System.Drawing" />
72+
<Reference Include="System.IdentityModel" />
73+
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=6.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
74+
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.6.22.0\lib\net472\System.IdentityModel.Tokens.Jwt.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
77+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
78+
</Reference>
79+
<Reference Include="System.Security" />
80+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
81+
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
82+
</Reference>
83+
<Reference Include="System.Windows.Forms" />
84+
<Reference Include="System.Xml.Linq" />
85+
<Reference Include="System.Data.DataSetExtensions" />
86+
<Reference Include="Microsoft.CSharp" />
87+
<Reference Include="System.Data" />
88+
<Reference Include="System.Net.Http" />
89+
<Reference Include="System.Xml" />
90+
</ItemGroup>
91+
<ItemGroup>
92+
<Compile Include="CustomAccountCache.cs" />
93+
<Compile Include="CustomCacheItem.cs" />
94+
<Compile Include="CustomCacheKey.cs" />
95+
<Compile Include="Program.cs" />
96+
<Compile Include="Properties\AssemblyInfo.cs" />
97+
</ItemGroup>
98+
<ItemGroup>
99+
<None Include="App.config" />
100+
<None Include="packages.config" />
101+
</ItemGroup>
102+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
103+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31bf3856ad364e35" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-6.22.0.0" newVersion="6.22.0.0" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="Microsoft.Identity.Client" publicKeyToken="0a613f4dd989e8ae" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-4.46.1.0" newVersion="4.46.1.0" />
15+
</dependentAssembly>
16+
<dependentAssembly>
17+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
18+
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
19+
</dependentAssembly>
20+
</assemblyBinding>
21+
</runtime>
22+
</configuration>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Microsoft.Identity.Client;
2+
using Microsoft.VisualStudio.Services.Client.AccountManagement;
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
8+
namespace AccountProviderSample
9+
{
10+
public class CustomAccountCache : IAccountCache
11+
{
12+
// This is a simple in-memory cache. In a real scenario, you would want to use a persistent cache.
13+
private readonly ConcurrentDictionary<CustomCacheKey, CustomCacheItem> inMemoryCache = new ConcurrentDictionary<CustomCacheKey, CustomCacheItem>();
14+
15+
private string ClientId { get; }
16+
public string Authority { get; }
17+
18+
public CustomAccountCache(string clientId, string authority)
19+
{
20+
ClientId = clientId;
21+
Authority = authority;
22+
}
23+
24+
private CustomCacheKey ConstructKey(string[] scopes, string userIdentifier, string tenantId = null)
25+
{
26+
return new CustomCacheKey(scopes, userIdentifier, tenantId);
27+
}
28+
29+
public async Task<IAccountCacheItem> AcquireTokenInteractiveAsync(string[] scopes, Prompt prompt = default, string userIdentifier = null, string tenantId = null)
30+
{
31+
// This is a sub-optimal implementation, since the PublicApplicationClient could be reused.
32+
var builder = PublicClientApplicationBuilder.Create(ClientId).WithAuthority(Authority);
33+
34+
var application = builder.Build();
35+
var query = application.AcquireTokenInteractive(scopes);
36+
37+
if (!string.IsNullOrWhiteSpace(userIdentifier))
38+
{
39+
var account = await application.GetAccountAsync(userIdentifier);
40+
query.WithAccount(account);
41+
}
42+
43+
if (!string.IsNullOrWhiteSpace(tenantId))
44+
{
45+
query.WithTenantId(tenantId);
46+
}
47+
48+
var result = await query.ExecuteAsync();
49+
50+
if (result != null)
51+
{
52+
var key = ConstructKey(scopes, userIdentifier, tenantId);
53+
var value = new CustomCacheItem(result);
54+
inMemoryCache.AddOrUpdate(key, value, (k, v) => value);
55+
return value;
56+
}
57+
58+
return null;
59+
}
60+
61+
public async Task<IAccountCacheItem> AcquireTokenSilentAsync(string[] scopes, string userIdentifier, string tenantId = null)
62+
{
63+
var key = ConstructKey(scopes, userIdentifier, tenantId);
64+
if (inMemoryCache.TryGetValue(key, out var result))
65+
{
66+
return result;
67+
}
68+
69+
return await AcquireTokenInteractiveAsync(scopes, Prompt.NoPrompt, userIdentifier, tenantId);
70+
}
71+
72+
public Task DeleteItemAsync(IAccountCacheItem token)
73+
{
74+
throw new NotImplementedException();
75+
}
76+
77+
public Task<string> GetAnyUserIdentifierAsync()
78+
{
79+
throw new NotImplementedException();
80+
}
81+
82+
public IEnumerable<IAccountCacheItem> GetItems()
83+
{
84+
throw new NotImplementedException();
85+
}
86+
87+
public Task<IEnumerable<IAccountCacheItem>> GetItemsAsync()
88+
{
89+
throw new NotImplementedException();
90+
}
91+
92+
public IEnumerable<IAccountCacheItem> GetVsoEndpointToken(IAccountCacheItem tokenCacheItem)
93+
{
94+
throw new NotImplementedException();
95+
}
96+
97+
public Task<IEnumerable<IAccountCacheItem>> GetVsoEndpointTokenAsync(IAccountCacheItem tokenCacheItem)
98+
{
99+
throw new NotImplementedException();
100+
}
101+
}
102+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.Identity.Client;
2+
using Microsoft.VisualStudio.Services.Client.AccountManagement;
3+
using System;
4+
5+
namespace AccountProviderSample
6+
{
7+
public class CustomCacheItem : IAccountCacheItem
8+
{
9+
public CustomCacheItem(AuthenticationResult result)
10+
{
11+
UniqueId = result.UniqueId;
12+
TenantId = result.TenantId;
13+
Username = result.Account.Username;
14+
InnerResult = result;
15+
// ...
16+
}
17+
18+
public string UniqueId { get; set; }
19+
20+
public string TenantId { get; set; }
21+
22+
public string Username { get; set; }
23+
24+
public string Environment { get; set; }
25+
26+
public string IdToken { get; set; }
27+
28+
public DateTimeOffset ExpiresOn { get; set; }
29+
30+
public string AccessToken { get; set; }
31+
32+
public AuthenticationResult InnerResult { get; set; }
33+
34+
public string GivenName { get; set; }
35+
36+
public string FamilyName { get; set; }
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AccountProviderSample
8+
{
9+
public struct CustomCacheKey
10+
{
11+
public CustomCacheKey(string[] scopes, string userIdentifier, string tenantId = null)
12+
{
13+
Scopes = scopes;
14+
UserIdentifier = userIdentifier;
15+
TenantId = tenantId;
16+
}
17+
18+
public string[] Scopes { get; }
19+
public string UserIdentifier { get; }
20+
public string TenantId { get; }
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.VisualStudio.Services.Client.AccountManagement;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
6+
namespace AccountProviderSample
7+
{
8+
internal class Program
9+
{
10+
private static string AadInstance = "https://login.microsoftonline.com/{0}"; // Change for other AAD instances
11+
private static string TenantId = "AAD_Tenant_ID";
12+
private static string UserId = "User_UPN";
13+
private static string ClientId = "AAD_Application_ID";
14+
private static string Authority = String.Format(CultureInfo.InvariantCulture, AadInstance, TenantId);
15+
16+
static void Main(string[] args)
17+
{
18+
var accountProvider = new VSAccountProvider(null);
19+
accountProvider.SetAccountCache(new CustomAccountCache(ClientId, Authority));
20+
21+
// This is the scope for the Azure DevOps Resource. Feel free to replace with your desired scopes.
22+
var scopes = new string[] { "499b84ac-1321-427f-aa17-267ca6975798/user_impersonation" };
23+
24+
// This is the handle of the window that will be used to display the authentication dialog.
25+
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
26+
var accountKey = new AccountKey(UserId, Guid.Parse(TenantId));
27+
var result = accountProvider.AcquireTokenAsync(scopes, TenantId, UserId, handle, accountKey).GetAwaiter().GetResult();
28+
29+
Console.WriteLine(result.AccessToken);
30+
}
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("AccountProviderSample")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft Corp.")]
12+
[assembly: AssemblyProduct("AccountProviderSample")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft Corp. 2023")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("3ee4ced9-4158-468c-9f88-1035ca95a92c")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)