11using System ;
2- using System . Collections ;
2+ using System . Collections . Generic ;
33using System . Globalization ;
44using System . IO ;
55using System . Net ;
66using System . Net . Http ;
77using System . Net . Http . Headers ;
88using System . Text ;
9+ using System . Text . Json ;
910using System . Threading . Tasks ;
1011using DSInternals . Common . Data ;
1112using DSInternals . Common . Exceptions ;
12- using Newtonsoft . Json ;
13+ using DSInternals . Common . Serialization ;
1314
1415namespace DSInternals . Common . AzureAD
1516{
@@ -31,7 +32,6 @@ public class AzureADClient : IDisposable
3132 private string _tenantId ;
3233 private HttpClient _httpClient ;
3334 private readonly string _batchSizeParameter ;
34- private JsonSerializer _jsonSerializer = JsonSerializer . CreateDefault ( ) ;
3535
3636 public AzureADClient ( string accessToken , Guid ? tenantId = null , int batchSize = MaxBatchSize )
3737 {
@@ -117,17 +117,17 @@ public async Task SetUserAsync(string userPrincipalName, KeyCredential[] keyCred
117117 // Vaidate the input
118118 Validator . AssertNotNullOrEmpty ( userPrincipalName , nameof ( userPrincipalName ) ) ;
119119
120- var properties = new Hashtable ( ) { { KeyCredentialAttributeName , keyCredentials } } ;
120+ var properties = new Dictionary < string , object > { { KeyCredentialAttributeName , keyCredentials } } ;
121121 await SetUserAsync ( userPrincipalName , properties ) . ConfigureAwait ( false ) ;
122122 }
123123
124124 public async Task SetUserAsync ( Guid objectId , KeyCredential [ ] keyCredentials )
125125 {
126- var properties = new Hashtable ( ) { { KeyCredentialAttributeName , keyCredentials } } ;
126+ var properties = new Dictionary < string , object > { { KeyCredentialAttributeName , keyCredentials } } ;
127127 await SetUserAsync ( objectId . ToString ( ) , properties ) . ConfigureAwait ( false ) ;
128128 }
129129
130- private async Task SetUserAsync ( string userIdentifier , Hashtable properties )
130+ private async Task SetUserAsync ( string userIdentifier , Dictionary < string , object > properties )
131131 {
132132 // Build the request uri
133133 var url = new StringBuilder ( ) ;
@@ -137,7 +137,7 @@ private async Task SetUserAsync(string userIdentifier, Hashtable properties)
137137 // TODO: Switch to HttpMethod.Patch after migrating to .NET Standard 2.1 / .NET 5
138138 using ( var request = new HttpRequestMessage ( new HttpMethod ( "PATCH" ) , url . ToString ( ) ) )
139139 {
140- request . Content = new StringContent ( JsonConvert . SerializeObject ( properties ) , Encoding . UTF8 , JsonContentType ) ;
140+ request . Content = new StringContent ( JsonSerializer . Serialize ( properties , DsiJson . Options ) , Encoding . UTF8 , JsonContentType ) ;
141141 await SendODataRequest < object > ( request ) . ConfigureAwait ( false ) ;
142142 }
143143 }
@@ -155,30 +155,26 @@ private async Task<T> SendODataRequest<T>(HttpRequestMessage request)
155155 }
156156
157157 using ( var responseStream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) )
158- using ( var streamReader = new StreamReader ( responseStream ) )
159158 {
160159 if ( s_odataContentType . MediaType . Equals ( response . Content . Headers . ContentType . MediaType , StringComparison . InvariantCultureIgnoreCase ) )
161160 {
162- // The response is a JSON document
163- using ( var jsonTextReader = new JsonTextReader ( streamReader ) )
161+ if ( response . StatusCode == HttpStatusCode . OK )
164162 {
165- if ( response . StatusCode == HttpStatusCode . OK )
166- {
167- return _jsonSerializer . Deserialize < T > ( jsonTextReader ) ;
168- }
169- else
170- {
171- // Translate OData response to an exception
172- var error = _jsonSerializer . Deserialize < OdataErrorResponse > ( jsonTextReader ) ;
173- throw error . GetException ( ) ;
174- }
163+ return await JsonSerializer . DeserializeAsync < T > ( responseStream , DsiJson . Options ) . ConfigureAwait ( false ) ;
164+ }
165+ else
166+ {
167+ var error = await JsonSerializer . DeserializeAsync < OdataErrorResponse > ( responseStream , DsiJson . Options ) . ConfigureAwait ( false ) ;
168+ throw error . GetException ( ) ;
175169 }
176170 }
177171 else
178172 {
179- // The response is not a JSON document, so we parse its first line as message text
180- string message = await streamReader . ReadLineAsync ( ) . ConfigureAwait ( false ) ;
181- throw new GraphApiException ( message , response . StatusCode . ToString ( ) ) ;
173+ using ( var streamReader = new StreamReader ( responseStream ) )
174+ {
175+ string message = await streamReader . ReadLineAsync ( ) . ConfigureAwait ( false ) ;
176+ throw new GraphApiException ( message , response . StatusCode . ToString ( ) ) ;
177+ }
182178 }
183179 }
184180 }
0 commit comments