22// Licensed under the MIT License.
33
44using System ;
5+ using System . Linq ;
56using System . Runtime . InteropServices ;
7+ using System . Threading ;
68using System . Threading . Tasks ;
79using Azure . Core ;
810using NUnit . Framework ;
@@ -11,6 +13,8 @@ namespace Azure.Identity.BrokeredAuthentication.Tests
1113{
1214 public class ManualInteractiveBrowserCredentialBrokerTests
1315 {
16+ private static TokenRequestContext context = new TokenRequestContext ( new string [ ] { "https://vault.azure.net/.default" } ) ;
17+
1418 [ DllImport ( "user32.dll" ) ]
1519 private static extern IntPtr GetForegroundWindow ( ) ;
1620
@@ -28,5 +32,62 @@ public async Task AuthenticateWithBrokerAsync()
2832
2933 Assert . NotNull ( token . Token ) ;
3034 }
35+
36+ [ Test ]
37+ [ Ignore ( "This test is an integration test which can only be run with user interaction" ) ]
38+ public void AuthenticateWithBrokerAsyncWithSTA ( [ Values ( true , false ) ] bool isAsync )
39+ {
40+ var assemblies = System . Reflection . Assembly . GetExecutingAssembly ( ) . GetReferencedAssemblies ( ) . FirstOrDefault ( a => a . Name == "System.Windows.Forms" ) ;
41+ if ( assemblies != null )
42+ {
43+ throw new Exception ( "has winforms" ) ;
44+ }
45+ ManualResetEventSlim evt = new ( ) ;
46+ Thread thread = new Thread ( async ( ) =>
47+ {
48+ // do something with retVal
49+
50+ Console . WriteLine ( $ "Thread { Thread . CurrentThread . GetApartmentState ( ) } ") ;
51+ IntPtr parentWindowHandle = GetForegroundWindow ( ) ;
52+ var options = new InteractiveBrowserCredentialBrokerOptions ( parentWindowHandle )
53+ {
54+ TokenCachePersistenceOptions = new ( )
55+ } ;
56+
57+ var cred = new InteractiveBrowserCredential ( options ) ;
58+ var authRecord = isAsync ? await cred . AuthenticateAsync ( context ) : cred . Authenticate ( context ) ;
59+ options . AuthenticationRecord = authRecord ;
60+ AccessToken token = isAsync ? await cred . GetTokenAsync ( context ) . ConfigureAwait ( false ) : cred . GetToken ( context ) ;
61+ Console . WriteLine ( "got token" ) ;
62+ evt . Set ( ) ;
63+ // });
64+ } ) ;
65+
66+ Thread thread2 = new Thread ( async ( ) =>
67+ {
68+ // do something with retVal
69+
70+ Console . WriteLine ( $ "Thread { Thread . CurrentThread . GetApartmentState ( ) } ") ;
71+ IntPtr parentWindowHandle = GetForegroundWindow ( ) ;
72+ var options = new InteractiveBrowserCredentialBrokerOptions ( parentWindowHandle )
73+ {
74+ TokenCachePersistenceOptions = new ( )
75+ } ;
76+
77+ var cred = new InteractiveBrowserCredential ( options ) ;
78+ var authRecord = isAsync ? await cred . AuthenticateAsync ( context ) : cred . Authenticate ( context ) ;
79+ options . AuthenticationRecord = authRecord ;
80+ AccessToken token = isAsync ? await cred . GetTokenAsync ( context ) . ConfigureAwait ( false ) : cred . GetToken ( context ) ;
81+ Console . WriteLine ( "got token" ) ;
82+ thread . Start ( ) ;
83+ // });
84+ } ) ;
85+ #pragma warning disable CA1416 // Validate platform compatibility
86+ thread . SetApartmentState ( ApartmentState . STA ) ;
87+ thread2 . SetApartmentState ( ApartmentState . STA ) ;
88+ #pragma warning restore CA1416 // Validate platform compatibility
89+ thread2 . Start ( ) ;
90+ evt . Wait ( 10000 ) ;
91+ }
3192 }
3293}
0 commit comments