ResumptionCookie.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.IO;
36 using System.IO.Compression;
37 using System.Runtime.Serialization.Formatters.Binary;
38 using Microsoft.Bot.Connector;
39 using Newtonsoft.Json;
40 
41 namespace Microsoft.Bot.Builder.Dialogs
42 {
46  [Obsolete("Use ConversationReference.")]
47  [Serializable]
48  public sealed class ResumptionCookie : IEquatable<ResumptionCookie>
49  {
53  public IAddress Address { get; set; }
54 
58  public string UserName { set; get; }
59 
66  public bool IsTrustedServiceUrl { private set; get; }
67 
71  public bool IsGroup { set; get; }
72 
76  public string Locale { set; get; }
77 
85  [JsonConstructor]
86  public ResumptionCookie(Address address, string userName, bool isGroup, string locale)
87  {
88  this.Address = address;
89  this.UserName = userName;
90  this.IsGroup = isGroup;
91  this.Locale = locale;
92  this.IsTrustedServiceUrl = MicrosoftAppCredentials.IsTrustedServiceUrl(address.ServiceUrl);
93  }
94 
100  : this
101  (
102  address: Dialogs.Address.FromActivity(msg),
103  userName: msg.From?.Name,
104  isGroup: msg.Conversation?.IsGroup ?? false,
105  locale: msg.Locale
106  )
107  {
108  }
109 
110  public bool Equals(ResumptionCookie other)
111  {
112  return other != null
113  && object.Equals(this.Address, other.Address)
114  && object.Equals(this.UserName, other.UserName)
115  && this.IsTrustedServiceUrl == other.IsTrustedServiceUrl
116  && this.IsGroup == other.IsGroup
117  && object.Equals(this.Locale, other.Locale);
118  }
119 
120  public override bool Equals(object other)
121  {
122  return this.Equals(other as ResumptionCookie);
123  }
124 
125  public override int GetHashCode()
126  {
127  return this.Address.GetHashCode();
128  }
129 
135  {
136  return new Activity
137  {
138  Id = Guid.NewGuid().ToString(),
139  Recipient = new ChannelAccount
140  {
141  Id = this.Address.BotId
142  },
143  ChannelId = this.Address.ChannelId,
144  ServiceUrl = this.Address.ServiceUrl,
146  {
147  Id = this.Address.ConversationId,
148  IsGroup = this.IsGroup
149  },
150  From = new ChannelAccount
151  {
152  Id = this.Address.UserId,
153  Name = this.UserName
154  },
155  Locale = this.Locale
156  };
157  }
158 
164  public static ResumptionCookie GZipDeserialize(string str)
165  {
166  byte[] bytes = Convert.FromBase64String(str);
167 
168  using (var stream = new MemoryStream(bytes))
169  using (var gz = new GZipStream(stream, CompressionMode.Decompress))
170  {
171  return (ResumptionCookie)(new BinaryFormatter().Deserialize(gz));
172  }
173  }
174  }
175 
176  public partial class Extensions
177  {
183  [Obsolete("Use ConversationReference.")]
184  public static string GZipSerialize(this ResumptionCookie resumptionCookie)
185  {
186  using (var cmpStream = new MemoryStream())
187  using (var stream = new GZipStream(cmpStream, CompressionMode.Compress))
188  {
189  new BinaryFormatter().Serialize(stream, resumptionCookie);
190  stream.Close();
191  return Convert.ToBase64String(cmpStream.ToArray());
192  }
193  }
194  }
195 }
static string GZipSerialize(this ResumptionCookie resumptionCookie)
Binary serializes ResumptionCookie using GZipStream.
Namespace for the Microsoft Bot Connector SDK.
Channel account information for a conversation
The key that minimally and completely identifies a bot&#39;s conversation with a user on a channel...
Definition: Address.cs:45
An Activity is the basic communication type for the Bot Framework 3.0 protocol
Definition: ActivityEx.cs:18
static bool IsTrustedServiceUrl(string serviceUrl)
Checks if the service url is for a trusted host or not.
The key that minimally and completely identifies a bot&#39;s conversation with a user on a channel...
Definition: Address.cs:58
Channel account information needed to route a message