@@ -105,8 +105,21 @@ private async Task<bool> AuthenticateAsync(Control? owner)
105105
106106 try
107107 {
108- var currentAuthStatus = await _client . GetAuthStatusAsync ( ) ;
109- if ( currentAuthStatus . IsAuthenticated ) return true ;
108+ bool isAuthenticated ;
109+ try
110+ {
111+ var currentAuthStatus = await _client . GetAuthStatusAsync ( ) ;
112+ isAuthenticated = currentAuthStatus . IsAuthenticated ;
113+ }
114+ catch ( IOException ex ) when ( ex . InnerException ? . GetType ( ) . Name == "RemoteInvocationException" &&
115+ ex . Message . Contains ( "401" ) )
116+ {
117+ // Treat 401 authentication errors as unauthenticated
118+ ContainerLocator . Container . Resolve < ILogger > ( ) . LogWarning ( ex , "Authentication check failed with 401, treating as unauthenticated." ) ;
119+ isAuthenticated = false ;
120+ }
121+
122+ if ( isAuthenticated ) return true ;
110123
111124 var cliPath = settingsService . GetSettingValue < string > ( CopilotModule . CopilotCliSettingKey ) ;
112125
@@ -189,9 +202,21 @@ public async Task<bool> InitializeAsync()
189202 CliPath = cliPath
190203 } ) ;
191204
192- var authStatus = await _client . GetAuthStatusAsync ( ) ;
205+ bool isAuthenticated ;
206+ try
207+ {
208+ var authStatus = await _client . GetAuthStatusAsync ( ) ;
209+ isAuthenticated = authStatus . IsAuthenticated ;
210+ }
211+ catch ( IOException ex ) when ( ex . InnerException ? . GetType ( ) . Name == "RemoteInvocationException" &&
212+ ex . Message . Contains ( "401" ) )
213+ {
214+ // Treat 401 authentication errors as unauthenticated
215+ ContainerLocator . Container . Resolve < ILogger > ( ) . LogWarning ( ex , "Authentication check failed with 401, treating as unauthenticated." ) ;
216+ isAuthenticated = false ;
217+ }
193218
194- if ( ! authStatus . IsAuthenticated )
219+ if ( ! isAuthenticated )
195220 {
196221 StatusChanged ? . Invoke ( this , new StatusEvent ( false , "Not Authenticated" ) ) ;
197222 EventReceived ? . Invoke ( this , new ChatButtonEvent (
@@ -252,12 +277,7 @@ private async Task InitializeSessionAsync()
252277
253278 var sessionId = _requestedSessionId ;
254279 _requestedSessionId = null ;
255-
256- if ( string . IsNullOrWhiteSpace ( sessionId ) && ! _forceNewSession )
257- {
258- sessionId = await _client . GetLastSessionIdAsync ( ) ;
259- }
260-
280+
261281 if ( string . IsNullOrWhiteSpace ( sessionId ) )
262282 {
263283 var tools = toolProvider . GetTools ( ) ;
0 commit comments