Skip to content

Commit 00d4537

Browse files
DharshanBJbgavrilMS
authored andcommitted
update
1 parent fcae881 commit 00d4537

File tree

1 file changed

+31
-44
lines changed

1 file changed

+31
-44
lines changed

src/client/Microsoft.Identity.Client.Desktop/WebView2WebUi/WebView2WebUi.cs

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public async Task<AuthorizationResult> AcquireAuthorizationAsync(
4949
{
5050
result = await InvokeEmbeddedWebviewAsync(authorizationUri, redirectUri, cancellationToken).ConfigureAwait(false);
5151
});
52+
#else
53+
var sendAuthorizeRequest = new Action(() =>
54+
{
55+
result = InvokeEmbeddedWebview(authorizationUri, redirectUri, cancellationToken);
56+
});
57+
#endif
5258

5359
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
5460
{
5561
if (_parent.SynchronizationContext != null)
5662
{
63+
#if WINUI3
5764
var tcs = new TaskCompletionSource<AuthorizationResult>();
5865

5966
_parent.SynchronizationContext.Post((state) =>
@@ -83,50 +90,7 @@ public async Task<AuthorizationResult> AcquireAuthorizationAsync(
8390
}, tcs);
8491

8592
return await tcs.Task.ConfigureAwait(false);
86-
}
87-
else
88-
{
89-
using (var staTaskScheduler = new StaTaskScheduler(1))
90-
{
91-
try
92-
{
93-
Task.Factory.StartNew(
94-
sendAuthorizeRequest,
95-
cancellationToken,
96-
TaskCreationOptions.None,
97-
staTaskScheduler).Wait(cancellationToken);
98-
}
99-
catch (AggregateException ae)
100-
{
101-
requestContext.Logger.ErrorPii(ae.InnerException);
102-
Exception innerException = ae.InnerExceptions[0];
103-
104-
if (innerException is AggregateException exception)
105-
{
106-
innerException = exception.InnerExceptions[0];
107-
}
108-
109-
throw innerException;
110-
}
111-
}
112-
}
113-
}
114-
else
115-
{
116-
await sendAuthorizeRequest().ConfigureAwait(false);
117-
}
118-
119-
return result;
12093
#else
121-
var sendAuthorizeRequest = new Action(() =>
122-
{
123-
result = InvokeEmbeddedWebview(authorizationUri, redirectUri, cancellationToken);
124-
});
125-
126-
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
127-
{
128-
if (_parent.SynchronizationContext != null)
129-
{
13094
var sendAuthorizeRequestWithTcs = new Action<object>((tcs) =>
13195
{
13296
try
@@ -147,22 +111,41 @@ public async Task<AuthorizationResult> AcquireAuthorizationAsync(
147111
_parent.SynchronizationContext.Post(
148112
new SendOrPostCallback(sendAuthorizeRequestWithTcs), tcs2);
149113
await tcs2.Task.ConfigureAwait(false);
114+
#endif
150115
}
151116
else
152117
{
153118
using (var staTaskScheduler = new StaTaskScheduler(1))
154119
{
155120
try
156121
{
122+
#if WINUI3
123+
await Task.Factory.StartNew(
124+
sendAuthorizeRequest,
125+
cancellationToken,
126+
TaskCreationOptions.None,
127+
staTaskScheduler).Unwrap().ConfigureAwait(false);
128+
#else
157129
Task.Factory.StartNew(
158130
sendAuthorizeRequest,
159131
cancellationToken,
160132
TaskCreationOptions.None,
161133
staTaskScheduler).Wait(cancellationToken);
134+
#endif
162135
}
163136
catch (AggregateException ae)
164137
{
165138
requestContext.Logger.ErrorPii(ae.InnerException);
139+
#if WINUI3
140+
Exception innerException = ae.InnerExceptions[0];
141+
142+
if (innerException is AggregateException exception)
143+
{
144+
innerException = exception.InnerExceptions[0];
145+
}
146+
147+
throw innerException;
148+
#else
166149
// Any exception thrown as a result of running task will cause AggregateException to be thrown with
167150
// actual exception as inner.
168151
Exception innerException = ae.InnerExceptions[0];
@@ -174,17 +157,21 @@ public async Task<AuthorizationResult> AcquireAuthorizationAsync(
174157
}
175158

176159
throw innerException;
160+
#endif
177161
}
178162
}
179163
}
180164
}
181165
else
182166
{
167+
#if WINUI3
168+
await sendAuthorizeRequest().ConfigureAwait(false);
169+
#else
183170
sendAuthorizeRequest();
171+
#endif
184172
}
185173

186174
return result;
187-
#endif
188175
}
189176

190177
public Uri UpdateRedirectUri(Uri redirectUri)

0 commit comments

Comments
 (0)