1 //----------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation.
4 // All rights reserved.
6 // This code is licensed under the MIT License.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files(the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions :
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 //------------------------------------------------------------------------------
29 using System.Collections.Generic;
32 using System.Runtime.Serialization;
33 using System.Runtime.Serialization.Json;
34 using System.Security;
36 using System.Threading.Tasks;
38 namespace Microsoft.IdentityModel.Clients.ActiveDirectory
41 internal class AuthenticationResultEx
44 public AuthenticationResult Result { get; set; }
47 /// Gets the Refresh Token associated with the requested Access Token. Note: not all operations will return a Refresh Token.
50 public string RefreshToken { get; set; }
53 /// Gets a value indicating whether the refresh token can be used for requesting access token for other resources.
55 internal bool IsMultipleResourceRefreshToken
59 return (!string.IsNullOrWhiteSpace(this.RefreshToken) && !string.IsNullOrWhiteSpace(this.ResourceInResponse));
63 // This is only needed for AcquireTokenByAuthorizationCode in which parameter resource is optional and we need
64 // to get it from the STS response.
66 internal string ResourceInResponse { get; set; }
70 /// Serializes the object to a JSON string
72 /// <returns>Deserialized authentication result</returns>
73 public static AuthenticationResultEx Deserialize(string serializedObject)
75 AuthenticationResultEx resultEx;
76 var serializer = new DataContractJsonSerializer(typeof(AuthenticationResultEx));
77 byte[] serializedObjectBytes = Encoding.UTF8.GetBytes(serializedObject);
78 using (var stream = new MemoryStream(serializedObjectBytes))
80 resultEx = (AuthenticationResultEx)serializer.ReadObject(stream);
87 /// Serializes the object to a JSON string
89 /// <returns>Serialized authentication result</returns>
90 public string Serialize()
92 string serializedObject;
93 var serializer = new DataContractJsonSerializer(typeof(AuthenticationResultEx));
94 using (MemoryStream stream = new MemoryStream())
96 serializer.WriteObject(stream, this);
97 serializedObject = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Position);
100 return serializedObject;
103 internal Exception Exception { get; set; }
106 public string UserAssertionHash { get; set; }
108 internal AuthenticationResultEx Clone()
110 return new AuthenticationResultEx
112 UserAssertionHash = this.UserAssertionHash,
113 Exception = this.Exception,
114 RefreshToken = this.RefreshToken,
115 ResourceInResponse = this.ResourceInResponse,
116 Result = new AuthenticationResult(this.Result.AccessTokenType, this.Result.AccessToken, this.Result.ExpiresOn, this.Result.ExtendedExpiresOn)
118 ExtendedLifeTimeToken = this.Result.ExtendedLifeTimeToken,
119 IdToken = this.Result.IdToken,
120 TenantId = this.Result.TenantId,
121 UserInfo = new UserInfo(this.Result.UserInfo)