11using Newtonsoft . Json ;
22using Newtonsoft . Json . Linq ;
33using System ;
4+ using System . Collections . Generic ;
45using System . Linq ;
56using System . Net ;
67using System . Net . Http ;
@@ -60,6 +61,11 @@ public partial class WebUntisClient : IDisposable
6061 /// </summary>
6162 private string _sessonId ;
6263
64+ /// <summary>
65+ /// The school name for the sesson
66+ /// </summary>
67+ private string _schoolName ;
68+
6369 /// <summary>
6470 /// Auth token for api requests
6571 /// </summary>
@@ -169,6 +175,9 @@ public async Task<bool> LoginAsync(string server, string loginName, string usern
169175 throw responseModel . Error ;
170176 }
171177
178+ string headerValue = response . Headers . First ( header => header . Key == "Set-Cookie" ) . Value . ToArray ( ) [ 1 ] ; // Read additional school name header
179+ _schoolName = Regex . Match ( headerValue , "schoolname=\" (.+)\" ;" ) . Groups [ 1 ] . Value ;
180+
172181 _serverUrl = serverUrl ;
173182 _loginName = loginName ;
174183 _sessonId = responseModel . Result . SessionId ;
@@ -230,6 +239,7 @@ public async Task LogoutAsync(string id = "Logout", CancellationToken ct = defau
230239 } ;
231240 StringContent requestContent = new StringContent ( JsonConvert . SerializeObject ( requestModel ) , Encoding . UTF8 , "application/json" ) ;
232241 requestContent . Headers . Add ( "JSESSIONID" , _sessonId ) ;
242+ requestContent . Headers . Add ( "schoolname" , _schoolName ) ;
233243
234244 // Send request
235245 _ = await _client . PostAsync ( ServerUrl + "/WebUntis/jsonrpc.do" , requestContent , ct ) ;
@@ -238,6 +248,7 @@ public async Task LogoutAsync(string id = "Logout", CancellationToken ct = defau
238248 _serverUrl = null ;
239249 _loginName = null ;
240250 _sessonId = null ;
251+ _schoolName = null ;
241252 _bearerToken = null ;
242253 _loggedIn = false ;
243254 }
@@ -292,6 +303,7 @@ private async Task<TResult> MakeJSONRPCRequestAsync<TRequest, TResult>(string id
292303 } ;
293304 StringContent requestContent = new StringContent ( JsonConvert . SerializeObject ( requestModel ) , Encoding . UTF8 , "application/json" ) ;
294305 requestContent . Headers . Add ( "JSESSIONID" , _sessonId ) ;
306+ requestContent . Headers . Add ( "schoolname" , _schoolName ) ;
295307
296308 // Send request
297309 HttpResponseMessage response = await _client . PostAsync ( ServerUrl + requestUrl , requestContent , ct ) ;
@@ -347,6 +359,7 @@ private async Task<string> MakeAPIGetRequestAsync(string requestUrl, Cancellatio
347359 RequestUri = new Uri ( ServerUrl + requestUrl )
348360 } ;
349361 request . Headers . Add ( "JSESSIONID" , _sessonId ) ;
362+ request . Headers . Add ( "schoolname" , _schoolName ) ;
350363 request . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , _bearerToken ) ;
351364
352365 HttpResponseMessage response = await _client . SendAsync ( request , ct ) ;
@@ -376,6 +389,7 @@ private async Task<string> GetBearerTokenAsync(CancellationToken ct)
376389 RequestUri = new Uri ( ServerUrl + "/WebUntis/api/token/new" )
377390 } ;
378391 request . Headers . Add ( "JSESSIONID" , _sessonId ) ;
392+ request . Headers . Add ( "schoolname" , _schoolName ) ;
379393
380394 HttpResponseMessage response = await _client . SendAsync ( request , ct ) ;
381395
0 commit comments