Skip to content
Merged

FOCI #957

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add netfx console sample app that exercises foci
  • Loading branch information
bgavrilMS committed Mar 16, 2019
commit b3d3ad8bf97bf9d69d4895854cd0ff758e157742
14 changes: 7 additions & 7 deletions LibsAndSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreTestApp", "tests\dev
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesktopTestApp", "tests\devapps\DesktopTestApp\DesktopTestApp.csproj", "{3A8EC886-C71D-4384-BFE4-73378CC7293D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp", "samples\desktop\SampleApp\SampleApp.csproj", "{85397CDA-120A-4626-9865-AD79EBFAC794}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Identity.Test.UIAutomation.Infrastructure", "tests\Microsoft.Identity.Test.Core.UIAutomation\Microsoft.Identity.Test.UIAutomation.Infrastructure.csproj", "{3DC6EC76-D350-4D43-B206-A4CEC8AA36D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Identity.Test.Android.UIAutomation", "tests\Microsoft.Identity.Test.Android.UIAutomation\Microsoft.Identity.Test.Android.UIAutomation.csproj", "{5FF7878D-FEF2-4CAE-8ABA-36C01806D9CE}"
Expand Down Expand Up @@ -92,6 +90,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonCache.Test.Unit", "te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Identity.Test.Integration", "tests\Microsoft.Identity.Test.Integration\Microsoft.Identity.Test.Integration.csproj", "{C98AAC90-C9D4-4DAD-975F-A898A7F89B2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFxConsoleTestApp", "tests\devapps\NetFxConsoleTestApp\NetFxConsoleTestApp.csproj", "{24D2AF71-A01E-4450-8C3F-B9923A81134B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonCache.Test.AdalV5", "tests\CacheCompat\CommonCache.Test.AdalV5\CommonCache.Test.AdalV5.csproj", "{4DDB6B3A-F95A-4F34-B0AE-E3C909D68A11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonCache.Test.MsalJava", "tests\CacheCompat\CommonCache.Test.MsalJava\CommonCache.Test.MsalJava.csproj", "{B6BE589B-1DBB-4C36-803F-6D425723FA74}"
Expand Down Expand Up @@ -137,10 +137,6 @@ Global
{3A8EC886-C71D-4384-BFE4-73378CC7293D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A8EC886-C71D-4384-BFE4-73378CC7293D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A8EC886-C71D-4384-BFE4-73378CC7293D}.Release|Any CPU.Build.0 = Release|Any CPU
{85397CDA-120A-4626-9865-AD79EBFAC794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85397CDA-120A-4626-9865-AD79EBFAC794}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85397CDA-120A-4626-9865-AD79EBFAC794}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85397CDA-120A-4626-9865-AD79EBFAC794}.Release|Any CPU.Build.0 = Release|Any CPU
{3DC6EC76-D350-4D43-B206-A4CEC8AA36D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DC6EC76-D350-4D43-B206-A4CEC8AA36D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DC6EC76-D350-4D43-B206-A4CEC8AA36D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -209,6 +205,10 @@ Global
{C98AAC90-C9D4-4DAD-975F-A898A7F89B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C98AAC90-C9D4-4DAD-975F-A898A7F89B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C98AAC90-C9D4-4DAD-975F-A898A7F89B2D}.Release|Any CPU.Build.0 = Release|Any CPU
{24D2AF71-A01E-4450-8C3F-B9923A81134B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24D2AF71-A01E-4450-8C3F-B9923A81134B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24D2AF71-A01E-4450-8C3F-B9923A81134B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24D2AF71-A01E-4450-8C3F-B9923A81134B}.Release|Any CPU.Build.0 = Release|Any CPU
{4DDB6B3A-F95A-4F34-B0AE-E3C909D68A11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DDB6B3A-F95A-4F34-B0AE-E3C909D68A11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DDB6B3A-F95A-4F34-B0AE-E3C909D68A11}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -240,7 +240,6 @@ Global
{0B22271B-163F-429B-A472-4C7BF531CEE9} = {3761DEF3-458C-4CE2-BE94-4FC9D12217CC}
{72F0ADCA-3A0B-44AA-8129-21A1B27CD841} = {34BE693E-3496-45A4-B1D2-D3A0E068EEDB}
{3A8EC886-C71D-4384-BFE4-73378CC7293D} = {34BE693E-3496-45A4-B1D2-D3A0E068EEDB}
{85397CDA-120A-4626-9865-AD79EBFAC794} = {34BE693E-3496-45A4-B1D2-D3A0E068EEDB}
{3DC6EC76-D350-4D43-B206-A4CEC8AA36D4} = {9B0B5396-4D95-4C15-82ED-DC22B5A3123F}
{5FF7878D-FEF2-4CAE-8ABA-36C01806D9CE} = {9B0B5396-4D95-4C15-82ED-DC22B5A3123F}
{A181778D-5917-41CE-AA5F-7DAAA7B7F5BB} = {9B0B5396-4D95-4C15-82ED-DC22B5A3123F}
Expand All @@ -262,6 +261,7 @@ Global
{B6BE589B-1DBB-4C36-803F-6D425723FA74} = {58B95AA7-BE17-40F2-9C9A-FA27C282A10A}
{CA3F8F2E-7DF2-45A6-BC81-5DA5692FCC11} = {58B95AA7-BE17-40F2-9C9A-FA27C282A10A}
{2EAA7878-3E2A-4355-9841-430BF45EA8BD} = {58B95AA7-BE17-40F2-9C9A-FA27C282A10A}
{24D2AF71-A01E-4450-8C3F-B9923A81134B} = {34BE693E-3496-45A4-B1D2-D3A0E068EEDB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {020399A9-DC27-4B82-9CAA-EF488665AC27}
Expand Down
6 changes: 6 additions & 0 deletions tests/devapps/NetFxConsoleTestApp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
59 changes: 59 additions & 0 deletions tests/devapps/NetFxConsoleTestApp/NetFxConsoleTestApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{24D2AF71-A01E-4450-8C3F-B9923A81134B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>NetFxConsoleTestApp</RootNamespace>
<AssemblyName>NetFxConsoleTestApp</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.Identity.Client\Microsoft.Identity.Client.csproj">
<Project>{60117a9b-4bb8-472e-bfff-52cbf67ca95a}</Project>
<Name>Microsoft.Identity.Client</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
244 changes: 244 additions & 0 deletions tests/devapps/NetFxConsoleTestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.AppConfig;

namespace NetCoreTestApp
{
public class Program
{
// TODO: replace with FOCI family members IDs
private const string FAMILY_MEMBER_1 = "";
private const string FAMILY_MEMBER_2 = "";

private static readonly string[] s_scopes = new[] { "https://graph.microsoft.com/.default" };

private static readonly string s_cacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin";

// These 2 apps share the cache
private static IPublicClientApplication s_pca1;
private static IPublicClientApplication s_pca2;

public static void Main(string[] args)
{
s_pca1 = PublicClientApplicationBuilder
.Create(FAMILY_MEMBER_1)
.WithLogging(Log, LogLevel.Verbose, true)
.Build();

s_pca2 = PublicClientApplicationBuilder
.Create(FAMILY_MEMBER_2)
.WithLogging(Log, LogLevel.Verbose, true)
.Build();


SetCacheSerializationToFile(s_pca1);

RunConsoleAppLogicAsync().Wait();
}

private static void SetCacheSerializationToFile(IPublicClientApplication pca1)
{
pca1.UserTokenCache.SetBeforeAccess(notificationArgs =>
{
notificationArgs.TokenCache.DeserializeMsalV3(File.Exists(s_cacheFilePath)
? File.ReadAllBytes(s_cacheFilePath)
: null);
});
pca1.UserTokenCache.SetAfterAccess(notificationArgs =>
{
// if the access operation resulted in a cache update
if (notificationArgs.HasStateChanged)
{
// reflect changes in the persistent store
File.WriteAllBytes(s_cacheFilePath, notificationArgs.TokenCache.SerializeMsalV3());
}
});
}

private static async Task RunConsoleAppLogicAsync()
{
while (true)
{
Console.Clear();

await DisplayAccountsAsync(s_pca1).ConfigureAwait(false);

// display menu
Console.WriteLine(@"
1. Acquire Token App1
2. Acquire Token App2
3. Acquire Token Silent App1
4. Acquire Token Silent App2
5. Clear cache
0. Exit App
Enter your Selection: ");
int.TryParse(Console.ReadLine(), out var selection);

Task<AuthenticationResult> authTask = null;

try
{
switch (selection)
{
case 1:
authTask = GetInteractiveAuthTaskAsync(s_pca1);
FetchTokenAsync(s_pca2, authTask).GetAwaiter().GetResult();
break;
case 2:
authTask = GetInteractiveAuthTaskAsync(s_pca2);
FetchTokenAsync(s_pca2, authTask).GetAwaiter().GetResult();
break;
case 3:
authTask = GetSilentAuthTaskAsync(s_pca1);
FetchTokenAsync(s_pca1, authTask).GetAwaiter().GetResult();
break;
case 4:
authTask = GetSilentAuthTaskAsync(s_pca2);
FetchTokenAsync(s_pca2, authTask).GetAwaiter().GetResult();
break;

case 5:
var accounts = await s_pca1.GetAccountsAsync().ConfigureAwait(false);
foreach (var acc in accounts)
{
await s_pca1.RemoveAsync(acc).ConfigureAwait(false);
}

break;
case 0:
return;
default:
break;
}

}
catch (Exception ex)
{
Log(LogLevel.Error, ex.Message, false);
Log(LogLevel.Error, ex.StackTrace, false);
}

Console.WriteLine("\n\nHit 'ENTER' to continue...");
Console.ReadLine();
}
}

private static Task<AuthenticationResult> GetInteractiveAuthTaskAsync(IPublicClientApplication pca)
{
return pca.AcquireTokenInteractive(s_scopes, null).ExecuteAsync();
}


private static Task<AuthenticationResult> GetSilentAuthTaskAsync(IPublicClientApplication pca)
{
var accounts = pca.GetAccountsAsync().GetAwaiter().GetResult();
if (accounts.Count() > 1)
{
Log(LogLevel.Error, "Not expecting to handle multiple accounts", false);
return null;
}

return pca.AcquireTokenSilent(s_scopes, accounts.FirstOrDefault()).ExecuteAsync();
}



private static async Task FetchTokenAsync(IPublicClientApplication pca, Task<AuthenticationResult> authTask)
{
if (authTask == null)
{
return;
}

await authTask.ConfigureAwait(false);

Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Token is {0}", authTask.Result.AccessToken);
Console.ResetColor();


Console.BackgroundColor = ConsoleColor.DarkMagenta;
await DisplayAccountsAsync(pca).ConfigureAwait(false);

Console.ResetColor();
}



private static async Task DisplayAccountsAsync(IPublicClientApplication pca)
{
IEnumerable<IAccount> accounts = await pca.GetAccountsAsync().ConfigureAwait(false);

Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "For the public client, the tokenCache contains {0} token(s)", accounts.Count()));

foreach (var account in accounts)
{
Console.WriteLine("PCA account for: " + account.Username + "\n");
}
}

private static void Log(LogLevel level, string message, bool containsPii)
{
if (!containsPii)
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
}

switch (level)
{
case LogLevel.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogLevel.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogLevel.Verbose:
Console.ForegroundColor = ConsoleColor.Gray;
break;
default:
break;
}

Console.WriteLine($"{level} {message}");
Console.ResetColor();
}




}
}
Loading