- LoginWindow: 로그인/로그아웃/쿠키삭제를 전용 STA 스레드(자체 Dispatcher 펌프)에서 실행 → SignIn/SignInAsync가 MTA/STA 어느 스레드에서 호출돼도 동작 (RunOnDedicatedUiThreadAsync, AuthenticateAsync) - SsoClient: 로그인/로그아웃 창을 AuthenticateAsync로 호출, 크로스스레드 Owner 제거 - BaronSSO: SignIn/SignOut을 Task.Run(...).GetAwaiter().GetResult()로 정리, SignInAsync 데드락 주석 추가 - UserInfo: private set 프로퍼티에 [JsonInclude] 적용 → FromSsoFile 역직렬화 복원 정상화, LastAuthTime [JsonIgnore] - AuthTest 샘플 프로젝트 추가 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
156 lines
6.6 KiB
C#
156 lines
6.6 KiB
C#
using Microsoft.Web.WebView2.Core;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using WebView2 = Microsoft.Web.WebView2.Wpf.WebView2;
|
|
|
|
namespace BaronSoftware.SSO
|
|
{
|
|
/// <summary>
|
|
/// 인증 엔드포인트를 WebView2로 로드하고, redirect_uri로의 이동을 가로채
|
|
/// authorization code가 담긴 콜백 URL을 반환하는 로그인 창.
|
|
/// redirect_uri로 실제 페이지가 로드되기 전에 NavigationStarting에서 취소하므로,
|
|
/// localhost에 실제 서버를 띄울 필요가 없습니다.
|
|
/// </summary>
|
|
internal partial class LoginWindow : Window
|
|
{
|
|
private readonly string _authorizeUrl;
|
|
private readonly string _redirectUri;
|
|
private readonly TaskCompletionSource<string> _tcs =
|
|
new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
internal LoginWindow(string authorizeUrl, string redirectUri)
|
|
{
|
|
InitializeComponent();
|
|
_authorizeUrl = authorizeUrl;
|
|
_redirectUri = redirectUri;
|
|
|
|
Loaded += async (s, e) =>
|
|
{
|
|
try
|
|
{
|
|
await webview.EnsureCoreWebView2Async();
|
|
webview.CoreWebView2.NavigationStarting += OnNavigationStarting;
|
|
webview.CoreWebView2.Navigate(_authorizeUrl);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_tcs.TrySetException(ex);
|
|
Dispatcher.BeginInvoke(new Action(Close));
|
|
}
|
|
};
|
|
|
|
Closed += (_, _) => _tcs.TrySetException(new OperationCanceledException());
|
|
}
|
|
|
|
private void OnNavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
|
|
{
|
|
if (e.Uri.StartsWith(_redirectUri, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
e.Cancel = true; // 실제 localhost 로딩 차단
|
|
_tcs.TrySetResult(e.Uri);
|
|
Dispatcher.BeginInvoke(new Action(Close));
|
|
}
|
|
}
|
|
|
|
/// <summary>창을 띄우고 콜백 URL(code 포함)을 비동기로 반환.</summary>
|
|
internal Task<string> ShowAndGetRedirectAsync()
|
|
{
|
|
Show();
|
|
return _tcs.Task;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 호출 스레드가 MTA/STA 무엇이든, 전용 STA 스레드(자체 Dispatcher 펌프)에서
|
|
/// 로그인 창을 띄워 콜백 URL(code 또는 logout 복귀)을 비동기로 반환한다.
|
|
/// </summary>
|
|
internal static Task<string> AuthenticateAsync(string authorizeUrl, string redirectUri)
|
|
=> RunOnDedicatedUiThreadAsync(async () =>
|
|
{
|
|
var window = new LoginWindow(authorizeUrl, redirectUri);
|
|
return await window.ShowAndGetRedirectAsync();
|
|
});
|
|
|
|
/// <summary>
|
|
/// 로그인 WebView가 사용하는 프로필의 모든 쿠키(=SSO 세션 쿠키)를 삭제합니다.
|
|
/// 전용 STA 스레드에서 오프스크린 WebView2를 잠깐 띄워 동일 프로필의 쿠키를 비웁니다.
|
|
/// 이후 다음 로그인 시 세션이 없어 로그인 폼이 다시 표시됩니다.
|
|
/// </summary>
|
|
internal static Task ClearSessionCookiesAsync()
|
|
=> RunOnDedicatedUiThreadAsync(async () =>
|
|
{
|
|
var holder = new Window
|
|
{
|
|
Width = 1,
|
|
Height = 1,
|
|
Left = -32000,
|
|
Top = -32000,
|
|
WindowStyle = WindowStyle.None,
|
|
ShowInTaskbar = false,
|
|
ShowActivated = false,
|
|
Title = string.Empty,
|
|
};
|
|
var web = new WebView2();
|
|
holder.Content = web;
|
|
holder.Show(); // WebView2 초기화에 필요한 HWND 확보(화면 밖)
|
|
try
|
|
{
|
|
await web.EnsureCoreWebView2Async();
|
|
// DeleteAllCookies()는 즉시 반환이라, 디스크 반영 전에 Dispose되면 쿠키가 남아
|
|
// 다음 로그인이 SSO로 조용히 통과될 수 있다. 완료까지 await하는 ClearBrowsingDataAsync로
|
|
// SSO 세션 쿠키/사이트 데이터를 확실히 제거한다.
|
|
await web.CoreWebView2.Profile.ClearBrowsingDataAsync(
|
|
CoreWebView2BrowsingDataKinds.AllSite);
|
|
}
|
|
finally
|
|
{
|
|
web.Dispose();
|
|
holder.Close();
|
|
}
|
|
});
|
|
|
|
/// <summary>
|
|
/// 호출 스레드(MTA/STA)와 무관하게, 전용 STA 스레드에서 자체 Dispatcher 메시지펌프를 돌려
|
|
/// WPF/WebView2 UI 작업을 실행하고 그 결과를 Task로 반환한다.
|
|
/// (WPF 창은 STA + 메시지펌프가 필요하므로, MTA/Task.Run/콘솔에서도 안전하게 동작)
|
|
/// </summary>
|
|
internal static Task<T> RunOnDedicatedUiThreadAsync<T>(Func<Task<T>> work)
|
|
{
|
|
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
var thread = new Thread(() =>
|
|
{
|
|
try
|
|
{
|
|
// 이 전용 스레드에 Dispatcher 기반 SynchronizationContext를 설치 → await 연속이 같은 스레드로 복귀
|
|
SynchronizationContext.SetSynchronizationContext(
|
|
new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
|
|
|
|
work().ContinueWith(t =>
|
|
{
|
|
if (t.IsFaulted) tcs.TrySetException(t.Exception!.InnerExceptions);
|
|
else if (t.IsCanceled) tcs.TrySetCanceled();
|
|
else tcs.TrySetResult(t.Result);
|
|
|
|
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
|
|
}, TaskScheduler.FromCurrentSynchronizationContext());
|
|
|
|
Dispatcher.Run(); // 메시지펌프 시작 (작업 완료 시 InvokeShutdown으로 종료)
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
tcs.TrySetException(ex);
|
|
}
|
|
});
|
|
|
|
thread.IsBackground = true;
|
|
thread.SetApartmentState(ApartmentState.STA);
|
|
thread.Start();
|
|
return tcs.Task;
|
|
}
|
|
|
|
internal static Task RunOnDedicatedUiThreadAsync(Func<Task> work)
|
|
=> RunOnDedicatedUiThreadAsync<bool>(async () => { await work(); return true; });
|
|
}
|
|
}
|