@@ -31,13 +31,14 @@ public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssembl
31
31
[ Parameter ( ParameterSetName = Constants . AppParameterSet , Position = 3 ) ]
32
32
public string CertificateThumbprint { get ; set ; }
33
33
34
-
35
34
[ Parameter ( Position = 4 ) ]
36
35
public string TenantId { get ; set ; }
37
36
38
37
[ Parameter ( Position = 5 ) ]
39
38
public SwitchParameter ForceRefresh { get ; set ; }
40
39
40
+ private CancellationTokenSource cancellationTokenSource ;
41
+
41
42
protected override void BeginProcessing ( )
42
43
{
43
44
ValidateParameters ( ) ;
@@ -54,26 +55,26 @@ protected override void ProcessRecord()
54
55
base . ProcessRecord ( ) ;
55
56
56
57
IAuthContext authConfig = new AuthContext { TenantId = TenantId } ;
57
- CancellationToken cancellationToken = CancellationToken . None ;
58
58
59
59
if ( ParameterSetName == Constants . UserParameterSet )
60
60
{
61
61
// 2 mins timeout. 1 min < HTTP timeout.
62
62
TimeSpan authTimeout = new TimeSpan ( 0 , 0 , Constants . MaxDeviceCodeTimeOut ) ;
63
- CancellationTokenSource cts = new CancellationTokenSource ( authTimeout ) ;
64
- cancellationToken = cts . Token ;
65
-
63
+ cancellationTokenSource = new CancellationTokenSource ( authTimeout ) ;
66
64
authConfig . AuthType = AuthenticationType . Delegated ;
67
65
authConfig . Scopes = Scopes ?? new string [ ] { "User.Read" } ;
68
66
}
69
67
else
70
68
{
69
+ cancellationTokenSource = new CancellationTokenSource ( ) ;
71
70
authConfig . AuthType = AuthenticationType . AppOnly ;
72
71
authConfig . ClientId = ClientId ;
73
72
authConfig . CertificateThumbprint = CertificateThumbprint ;
74
73
authConfig . CertificateName = CertificateName ;
75
74
}
76
75
76
+ CancellationToken cancellationToken = cancellationTokenSource . Token ;
77
+
77
78
try
78
79
{
79
80
// Gets a static instance of IAuthenticationProvider when the client app hasn't changed.
@@ -84,7 +85,7 @@ protected override void ProcessRecord()
84
85
else
85
86
clientApplication = ( authProvider as ClientCredentialProvider ) . ClientApplication ;
86
87
87
- // Incremental scope consent without re-instanciating the auth provider. We will use a static instance.
88
+ // Incremental scope consent without re-instantiating the auth provider. We will use a static instance.
88
89
GraphRequestContext graphRequestContext = new GraphRequestContext ( ) ;
89
90
graphRequestContext . CancellationToken = cancellationToken ;
90
91
graphRequestContext . MiddlewareOptions = new Dictionary < string , IMiddlewareOption >
@@ -136,6 +137,7 @@ protected override void ProcessRecord()
136
137
137
138
protected override void StopProcessing ( )
138
139
{
140
+ cancellationTokenSource . Cancel ( ) ;
139
141
base . StopProcessing ( ) ;
140
142
}
141
143
0 commit comments