Files
baron-sso-sample/BaronSoftware.SSO/Features/LoginWindow/LoginWindow.xaml.cs
최준영 1c41230021 SSO 로그아웃(RP-Initiated end_session) 구현 및 세션 쿠키 정리 보강
- BaronSSO.SignOutAsync: id_token_hint 기반 end_session 로그아웃 + 로컬 세션 정리
- SsoClient.LogoutAsync: end_session_endpoint 이동 후 post_logout 복귀를 WebView에서 가로채기
- BaronSSOOption.PostLogoutRedirectUri 추가
- LoginWindow: 쿠키 삭제를 ClearBrowsingDataAsync(완료 대기)로 변경해 재로그인 자동 SSO 통과 방지
- UserInfo: IsFamily/IsCenter 대소문자 무시 비교를 StringComparer로 수정(빌드 오류 해소)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 12:25:41 +09:00

99 lines
3.8 KiB
C#

using Microsoft.Web.WebView2.Core;
using System.Windows;
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>
/// 로그인 WebView가 사용하는 프로필의 모든 쿠키(=SSO 세션 쿠키)를 삭제합니다.
/// 화면에 보이지 않는 오프스크린 WebView2를 잠깐 띄워 동일 프로필의 쿠키를 비웁니다.
/// 이후 다음 로그인 시 세션이 없어 로그인 폼이 다시 표시됩니다.
/// </summary>
internal static async Task ClearSessionCookiesAsync()
{
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();
}
}
}
}