Produced by Araxis Merge on 2/1/2017 2:56:43 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\Araxis_Merge_Comprasion\Pub_un\BTSSS_CIF_122016.zip\BTSSS_CIF_12_20_16\clean\CRM\trunk\SDK\SampleCode\CS\HelperCode | DeviceIdManager.cs | Tue Dec 20 19:51:44 2016 UTC |
| 2 | Wed Feb 1 19:56:43 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 995 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 2 | // | |||||
| 3 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 4 | // | |||||
| 5 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 6 | // | |||||
| 7 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 8 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 9 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 10 | // | |||||
| 11 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 12 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 13 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 14 | // PARTIC ULAR PURPO SE. | |||||
| 15 | // | |||||
| 16 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 17 | //<snippet DeviceIdMa nager> | |||||
| 18 | using Syst em; | |||||
| 19 | using Syst em.Collect ions.Gener ic; | |||||
| 20 | using Syst em.Compone ntModel; | |||||
| 21 | using Syst em.Diagnos tics.CodeA nalysis; | |||||
| 22 | using Syst em.Globali zation; | |||||
| 23 | using Syst em.IO; | |||||
| 24 | using Syst em.Net; | |||||
| 25 | using Syst em.Runtime .Serializa tion; | |||||
| 26 | using Syst em.Securit y.Cryptogr aphy; | |||||
| 27 | using Syst em.Service Model.Desc ription; | |||||
| 28 | using Syst em.Text; | |||||
| 29 | using Syst em.Xml; | |||||
| 30 | using Syst em.Xml.Ser ialization ; | |||||
| 31 | ||||||
| 32 | namespace Microsoft. Crm.Servic es.Utility | |||||
| 33 | { | |||||
| 34 | // / <summary > | |||||
| 35 | // / Manageme nt utility for the D evice Id | |||||
| 36 | // / </summar y> | |||||
| 37 | pu blic stati c class De viceIdMana ger | |||||
| 38 | { | |||||
| 39 | #reg ion Fields | |||||
| 40 | priv ate static readonly Random Ran domInstanc e = new Ra ndom(); | |||||
| 41 | ||||||
| 42 | publ ic const i nt MaxDevi ceNameLeng th = 24; | |||||
| 43 | publ ic const i nt MaxDevi cePassword Length = 2 4; | |||||
| 44 | #end region | |||||
| 45 | ||||||
| 46 | #reg ion Constr uctor | |||||
| 47 | stat ic DeviceI dManager() | |||||
| 48 | { | |||||
| 49 | Persis tToFile = true; | |||||
| 50 | } | |||||
| 51 | #end region | |||||
| 52 | ||||||
| 53 | #reg ion Proper ties | |||||
| 54 | /// <summary> | |||||
| 55 | /// Indicates whether th e register ed device credential s should b e persiste d to the d atabase | |||||
| 56 | /// </summary> | |||||
| 57 | publ ic static bool Persi stToFile { get; set; } | |||||
| 58 | ||||||
| 59 | /// <summary> | |||||
| 60 | /// Indicates that the c redentials should be persisted to the di sk if regi stration f ails with DeviceAlre adyExists. | |||||
| 61 | /// </summary> | |||||
| 62 | /// <remarks> | |||||
| 63 | /// If the dev ice alread y exists, there is a possibili ty that th e credenti als are th e same as the curren t credenti als that | |||||
| 64 | /// are being registered . This is especially true in a utomated e nvironment s where th e same cre dentials a re used co ntinually (to avoid | |||||
| 65 | /// registerin g spurious device cr edentials. | |||||
| 66 | /// </remarks> | |||||
| 67 | publ ic static bool Persi stIfDevice AlreadyExi sts { get; set; } | |||||
| 68 | #end region | |||||
| 69 | ||||||
| 70 | #reg ion Method s | |||||
| 71 | /// <summary> | |||||
| 72 | /// Loads the device cre dentials ( if they ex ist). | |||||
| 73 | /// </summary> | |||||
| 74 | /// <returns>< /returns> | |||||
| 75 | publ ic static ClientCred entials Lo adOrRegist erDevice() | |||||
| 76 | { | |||||
| 77 | return LoadOrReg isterDevic e(null); | |||||
| 78 | } | |||||
| 79 | ||||||
| 80 | /// <summary> | |||||
| 81 | /// Loads the device cre dentials ( if they ex ist). | |||||
| 82 | /// </summary> | |||||
| 83 | /// <param nam e="deviceN ame">Devic e name tha t should b e register ed</param> | |||||
| 84 | /// <param nam e="deviceP assword">D evice pass word that should be registered </param> | |||||
| 85 | publ ic static ClientCred entials Lo adOrRegist erDevice(s tring devi ceName, st ring devic ePassword) | |||||
| 86 | { | |||||
| 87 | return LoadOrReg isterDevic e(null, de viceName, devicePass word); | |||||
| 88 | } | |||||
| 89 | ||||||
| 90 | /// <summary> | |||||
| 91 | /// Loads the device cre dentials ( if they ex ist). | |||||
| 92 | /// </summary> | |||||
| 93 | /// <param nam e="issuerU ri">URL fo r the curr ent token issuer</pa ram> | |||||
| 94 | /// <remarks> | |||||
| 95 | /// The issuer Uri can be retrieved from the IServiceCo nfiguratio n interfac e's Curren tIssuer pr operty. | |||||
| 96 | /// </remarks> | |||||
| 97 | publ ic static ClientCred entials Lo adOrRegist erDevice(U ri issuerU ri) | |||||
| 98 | { | |||||
| 99 | return LoadOrReg isterDevic e(issuerUr i, null, n ull); | |||||
| 100 | } | |||||
| 101 | ||||||
| 102 | /// <summary> | |||||
| 103 | /// Loads the device cre dentials ( if they ex ist). | |||||
| 104 | /// </summary> | |||||
| 105 | /// <param nam e="issuerU ri">URL fo r the curr ent token issuer</pa ram> | |||||
| 106 | /// <param nam e="deviceN ame">Devic e name tha t should b e register ed</param> | |||||
| 107 | /// <param nam e="deviceP assword">D evice pass word that should be registered </param> | |||||
| 108 | /// <remarks> | |||||
| 109 | /// The issuer Uri can be retrieved from the IServiceCo nfiguratio n interfac e's Curren tIssuer pr operty. | |||||
| 110 | /// </remarks> | |||||
| 111 | publ ic static ClientCred entials Lo adOrRegist erDevice(U ri issuerU ri, string deviceNam e, string devicePass word) | |||||
| 112 | { | |||||
| 113 | Client Credential s credenti als = Load DeviceCred entials(is suerUri); | |||||
| 114 | if (nu ll == cred entials) | |||||
| 115 | { | |||||
| 116 | credenti als = Regi sterDevice (Guid.NewG uid(), iss uerUri, de viceName, devicePass word); | |||||
| 117 | } | |||||
| 118 | ||||||
| 119 | return credentia ls; | |||||
| 120 | } | |||||
| 121 | ||||||
| 122 | /// <summary> | |||||
| 123 | /// Registers the given device wit h Microsof t account with a ran dom applic ation ID | |||||
| 124 | /// </summary> | |||||
| 125 | /// <returns>C lientCrede ntials tha t were reg istered</r eturns> | |||||
| 126 | publ ic static ClientCred entials Re gisterDevi ce() | |||||
| 127 | { | |||||
| 128 | return RegisterD evice(Guid .NewGuid() ); | |||||
| 129 | } | |||||
| 130 | ||||||
| 131 | /// <summary> | |||||
| 132 | /// Registers the given device wit h Microsof t account | |||||
| 133 | /// </summary> | |||||
| 134 | /// <param nam e="applica tionId">ID for the a pplication </param> | |||||
| 135 | /// <returns>C lientCrede ntials tha t were reg istered</r eturns> | |||||
| 136 | publ ic static ClientCred entials Re gisterDevi ce(Guid ap plicationI d) | |||||
| 137 | { | |||||
| 138 | return RegisterD evice(appl icationId, (Uri)null ); | |||||
| 139 | } | |||||
| 140 | ||||||
| 141 | /// <summary> | |||||
| 142 | /// Registers the given device wit h Microsof t account | |||||
| 143 | /// </summary> | |||||
| 144 | /// <param nam e="applica tionId">ID for the a pplication </param> | |||||
| 145 | /// <param nam e="issuerU ri">URL fo r the curr ent token issuer</pa ram> | |||||
| 146 | /// <returns>C lientCrede ntials tha t were reg istered</r eturns> | |||||
| 147 | /// <remarks> | |||||
| 148 | /// The issuer Uri can be retrieved from the IServiceCo nfiguratio n interfac e's Curren tIssuer pr operty. | |||||
| 149 | /// </remarks> | |||||
| 150 | publ ic static ClientCred entials Re gisterDevi ce(Guid ap plicationI d, Uri iss uerUri) | |||||
| 151 | { | |||||
| 152 | return RegisterD evice(appl icationId, issuerUri , null, nu ll); | |||||
| 153 | } | |||||
| 154 | ||||||
| 155 | /// <summary> | |||||
| 156 | /// Registers the given device wit h Microsof t account | |||||
| 157 | /// </summary> | |||||
| 158 | /// <param nam e="applica tionId">ID for the a pplication </param> | |||||
| 159 | /// <param nam e="deviceN ame">Devic e name tha t should b e register ed</param> | |||||
| 160 | /// <param nam e="deviceP assword">D evice pass word that should be registered </param> | |||||
| 161 | /// <returns>C lientCrede ntials tha t were reg istered</r eturns> | |||||
| 162 | publ ic static ClientCred entials Re gisterDevi ce(Guid ap plicationI d, string deviceName , string d evicePassw ord) | |||||
| 163 | { | |||||
| 164 | return RegisterD evice(appl icationId, (Uri)null , deviceNa me, device Password); | |||||
| 165 | } | |||||
| 166 | ||||||
| 167 | /// <summary> | |||||
| 168 | /// Registers the given device wit h Microsof t account | |||||
| 169 | /// </summary> | |||||
| 170 | /// <param nam e="applica tionId">ID for the a pplication </param> | |||||
| 171 | /// <param nam e="issuerU ri">URL fo r the curr ent token issuer</pa ram> | |||||
| 172 | /// <param nam e="deviceN ame">Devic e name tha t should b e register ed</param> | |||||
| 173 | /// <param nam e="deviceP assword">D evice pass word that should be registered </param> | |||||
| 174 | /// <returns>C lientCrede ntials tha t were reg istered</r eturns> | |||||
| 175 | /// <remarks> | |||||
| 176 | /// The issuer Uri can be retrieved from the IServiceCo nfiguratio n interfac e's Curren tIssuer pr operty. | |||||
| 177 | /// </remarks> | |||||
| 178 | publ ic static ClientCred entials Re gisterDevi ce(Guid ap plicationI d, Uri iss uerUri, st ring devic eName, str ing device Password) | |||||
| 179 | { | |||||
| 180 | if (st ring.IsNul lOrEmpty(d eviceName) && !Persi stToFile) | |||||
| 181 | { | |||||
| 182 | throw ne w Argument NullExcept ion("devic eName", "I f PersistT oFile is f alse, then deviceNam e must be specified. "); | |||||
| 183 | } | |||||
| 184 | else i f (string. IsNullOrEm pty(device Name) != s tring.IsNu llOrEmpty( devicePass word)) | |||||
| 185 | { | |||||
| 186 | throw ne w Argument NullExcept ion("devic eName", "E ither devi ceName/dev icePasswor d should b oth be spe cified or they shoul d be null. "); | |||||
| 187 | } | |||||
| 188 | ||||||
| 189 | LiveDe vice devic e = Genera teDevice(d eviceName, devicePas sword); | |||||
| 190 | return RegisterD evice(appl icationId, issuerUri , device); | |||||
| 191 | } | |||||
| 192 | ||||||
| 193 | /// <summary> | |||||
| 194 | /// Loads the device's c redentials from the file syste m | |||||
| 195 | /// </summary> | |||||
| 196 | /// <returns>D evice Cred entials (i f set) or null</retu rns> | |||||
| 197 | publ ic static ClientCred entials Lo adDeviceCr edentials( ) | |||||
| 198 | { | |||||
| 199 | return LoadDevic eCredentia ls(null); | |||||
| 200 | } | |||||
| 201 | ||||||
| 202 | /// <summary> | |||||
| 203 | /// Loads the device's c redentials from the file syste m | |||||
| 204 | /// </summary> | |||||
| 205 | /// <param nam e="issuerU ri">URL fo r the curr ent token issuer</pa ram> | |||||
| 206 | /// <returns>D evice Cred entials (i f set) or null</retu rns> | |||||
| 207 | /// <remarks> | |||||
| 208 | /// The issuer Uri can be retrieved from the IServiceCo nfiguratio n interfac e's Curren tIssuer pr operty. | |||||
| 209 | /// </remarks> | |||||
| 210 | publ ic static ClientCred entials Lo adDeviceCr edentials( Uri issuer Uri) | |||||
| 211 | { | |||||
| 212 | //If t he credent ials shoul d not be p ersisted t o a file, then they won't be p resent on the disk. | |||||
| 213 | if (!P ersistToFi le) | |||||
| 214 | { | |||||
| 215 | return n ull; | |||||
| 216 | } | |||||
| 217 | ||||||
| 218 | Enviro nmentConfi guration e nvironment = Discove rEnvironme ntInternal (issuerUri ); | |||||
| 219 | ||||||
| 220 | LiveDe vice devic e = ReadEx istingDevi ce(environ ment); | |||||
| 221 | if (nu ll == devi ce || null == device .User) | |||||
| 222 | { | |||||
| 223 | return n ull; | |||||
| 224 | } | |||||
| 225 | ||||||
| 226 | return device.Us er.ToClien tCredentia ls(); | |||||
| 227 | } | |||||
| 228 | ||||||
| 229 | /// <summary> | |||||
| 230 | /// Discovers the Micros oft accoun t environm ent based on the Tok en Issuer | |||||
| 231 | /// </summary> | |||||
| 232 | publ ic static string Dis coverEnvir onment(Uri issuerUri ) | |||||
| 233 | { | |||||
| 234 | return DiscoverE nvironment Internal(i ssuerUri). Environmen t; | |||||
| 235 | } | |||||
| 236 | #end region | |||||
| 237 | ||||||
| 238 | #reg ion Privat e Methods | |||||
| 239 | priv ate static Environme ntConfigur ation Disc overEnviro nmentInter nal(Uri is suerUri) | |||||
| 240 | { | |||||
| 241 | if (nu ll == issu erUri) | |||||
| 242 | { | |||||
| 243 | return n ew Environ mentConfig uration(En vironmentT ype.LiveDe viceID, "l ogin.live. com", null ); | |||||
| 244 | } | |||||
| 245 | ||||||
| 246 | Dictio nary<Envir onmentType , string> searchList = new Dic tionary<En vironmentT ype, strin g>(); | |||||
| 247 | search List.Add(E nvironment Type.LiveD eviceID, " login.live "); | |||||
| 248 | search List.Add(E nvironment Type.OrgDe viceID, "l ogin.micro softonline "); | |||||
| 249 | ||||||
| 250 | foreac h (KeyValu ePair<Envi ronmentTyp e, string> searchPai r in searc hList) | |||||
| 251 | { | |||||
| 252 | if (issu erUri.Host .Length > searchPair .Value.Len gth && | |||||
| 253 | issuerUri. Host.Start sWith(sear chPair.Val ue, String Comparison .OrdinalIg noreCase)) | |||||
| 254 | { | |||||
| 255 | string env ironment = issuerUri .Host.Subs tring(sear chPair.Val ue.Length) ; | |||||
| 256 | ||||||
| 257 | //Parse ou t the envi ronment | |||||
| 258 | if ('-' == environme nt[0]) | |||||
| 259 | { | |||||
| 260 | in t separato rIndex = e nvironment .IndexOf(' .', 1); | |||||
| 261 | if (-1 != se paratorInd ex) | |||||
| 262 | { | |||||
| 263 | envi ronment = environmen t.Substrin g(1, separ atorIndex - 1); | |||||
| 264 | } | |||||
| 265 | el se | |||||
| 266 | { | |||||
| 267 | envi ronment = null; | |||||
| 268 | } | |||||
| 269 | } | |||||
| 270 | else | |||||
| 271 | { | |||||
| 272 | en vironment = null; | |||||
| 273 | } | |||||
| 274 | ||||||
| 275 | return new Environme ntConfigur ation(sear chPair.Key , issuerUr i.Host, en vironment) ; | |||||
| 276 | } | |||||
| 277 | } | |||||
| 278 | ||||||
| 279 | //In a ll other c ases the e nvironment is either not appli cable or i t is a pro duction sy stem | |||||
| 280 | return new Envir onmentConf iguration( Environmen tType.Live DeviceID, issuerUri. Host, null ); | |||||
| 281 | } | |||||
| 282 | ||||||
| 283 | priv ate static void Seri alize<T>(S tream stre am, T valu e) | |||||
| 284 | { | |||||
| 285 | XmlSer ializer se rializer = new XmlSe rializer(t ypeof(T), string.Emp ty); | |||||
| 286 | ||||||
| 287 | XmlSer ializerNam espaces xm lNamespace s = new Xm lSerialize rNamespace s(); | |||||
| 288 | xmlNam espaces.Ad d(string.E mpty, stri ng.Empty); | |||||
| 289 | ||||||
| 290 | serial izer.Seria lize(strea m, value, xmlNamespa ces); | |||||
| 291 | } | |||||
| 292 | ||||||
| 293 | priv ate static T Deseria lize<T>(st ring opera tionName, Stream str eam) | |||||
| 294 | { | |||||
| 295 | //Read the XML i nto memory so that t he data ca n be used in an exce ption if n ecessary | |||||
| 296 | using (StreamRea der reader = new Str eamReader( stream)) | |||||
| 297 | { | |||||
| 298 | return D eserialize <T>(operat ionName, r eader.Read ToEnd()); | |||||
| 299 | } | |||||
| 300 | } | |||||
| 301 | ||||||
| 302 | priv ate static T Deseria lize<T>(st ring opera tionName, string xml ) | |||||
| 303 | { | |||||
| 304 | //Atte mpt to des erialize t he data. I f deserial ization fa ils, inclu de the XML in the ex ception th at is thro wn for fur ther | |||||
| 305 | //inve stigation | |||||
| 306 | using (StringRea der reader = new Str ingReader( xml)) | |||||
| 307 | { | |||||
| 308 | try | |||||
| 309 | { | |||||
| 310 | XmlSeriali zer serial izer = new XmlSerial izer(typeo f(T), stri ng.Empty); | |||||
| 311 | return (T) serializer .Deseriali ze(reader) ; | |||||
| 312 | } | |||||
| 313 | catch (I nvalidOper ationExcep tion ex) | |||||
| 314 | { | |||||
| 315 | throw new InvalidOpe rationExce ption(stri ng.Format( CultureInf o.Invarian tCulture, | |||||
| 316 | "U nable to D eserialize XML (Oper ation = {0 }):{1}{2}" , operatio nName, Env ironment.N ewLine, xm l), ex); | |||||
| 317 | } | |||||
| 318 | } | |||||
| 319 | } | |||||
| 320 | ||||||
| 321 | priv ate static FileInfo GetDeviceF ile(Enviro nmentConfi guration e nvironment ) | |||||
| 322 | { | |||||
| 323 | return new FileI nfo(string .Format(Cu ltureInfo. InvariantC ulture, Li veIdConsta nts.FileNa meFormat, | |||||
| 324 | environm ent.Type, | |||||
| 325 | string.I sNullOrEmp ty(environ ment.Envir onment) ? null : "-" + environ ment.Envir onment.ToU pperInvari ant())); | |||||
| 326 | } | |||||
| 327 | ||||||
| 328 | priv ate static ClientCre dentials R egisterDev ice(Guid a pplication Id, Uri is suerUri, L iveDevice device) | |||||
| 329 | { | |||||
| 330 | Enviro nmentConfi guration e nvironment = Discove rEnvironme ntInternal (issuerUri ); | |||||
| 331 | ||||||
| 332 | Device Registrati onRequest request = new Device Registrati onRequest( applicatio nId, devic e); | |||||
| 333 | ||||||
| 334 | string url = str ing.Format (CultureIn fo.Invaria ntCulture, LiveIdCon stants.Reg istrationE ndpointUri Format, | |||||
| 335 | environm ent.HostNa me); | |||||
| 336 | ||||||
| 337 | Device Registrati onResponse response = ExecuteR egistratio nRequest(u rl, reques t); | |||||
| 338 | if (!r esponse.Is Success) | |||||
| 339 | { | |||||
| 340 | bool thr owExceptio n = true; | |||||
| 341 | if (Devi ceRegistra tionErrorC ode.Device AlreadyExi sts == res ponse.Erro r.Registra tionErrorC ode) | |||||
| 342 | { | |||||
| 343 | if (!Persi stToFile) | |||||
| 344 | { | |||||
| 345 | // If the fil e is not p ersisted, the regist ration wil l always o ccur (sinc e the cred entials ar e not | |||||
| 346 | // persisted to the dis k. However , the cred entials ma y already exist. To avoid an e xception b eing conti nually | |||||
| 347 | // processed by the cal ling user, DeviceAlr eadyExists will be i gnored if the creden tials are not persis ted to the disk. | |||||
| 348 | re turn devic e.User.ToC lientCrede ntials(); | |||||
| 349 | } | |||||
| 350 | else if (P ersistIfDe viceAlread yExists) | |||||
| 351 | { | |||||
| 352 | // This flag indicates that the | |||||
| 353 | th rowExcepti on = false ; | |||||
| 354 | } | |||||
| 355 | } | |||||
| 356 | ||||||
| 357 | if (thro wException ) | |||||
| 358 | { | |||||
| 359 | throw new DeviceRegi strationFa iledExcept ion(respon se.Error.R egistratio nErrorCode , response .ErrorSubC ode); | |||||
| 360 | } | |||||
| 361 | } | |||||
| 362 | ||||||
| 363 | if (Pe rsistToFil e || Persi stIfDevice AlreadyExi sts) | |||||
| 364 | { | |||||
| 365 | WriteDev ice(enviro nment, dev ice); | |||||
| 366 | } | |||||
| 367 | ||||||
| 368 | return device.Us er.ToClien tCredentia ls(); | |||||
| 369 | } | |||||
| 370 | ||||||
| 371 | priv ate static LiveDevic e Generate Device(str ing device Name, stri ng deviceP assword) | |||||
| 372 | { | |||||
| 373 | // If the device Name hasn' t been spe cified, it should be generated using ran dom charac ters. | |||||
| 374 | Device UserName u serNameCre dentials; | |||||
| 375 | if (st ring.IsNul lOrEmpty(d eviceName) ) | |||||
| 376 | { | |||||
| 377 | userName Credential s = Genera teDeviceUs erName(); | |||||
| 378 | } | |||||
| 379 | else | |||||
| 380 | { | |||||
| 381 | userName Credential s = new De viceUserNa me() { Dev iceName = deviceName , Decrypte dPassword = devicePa ssword }; | |||||
| 382 | } | |||||
| 383 | ||||||
| 384 | return new LiveD evice() { User = use rNameCrede ntials, Ve rsion = 1 }; | |||||
| 385 | } | |||||
| 386 | ||||||
| 387 | priv ate static LiveDevic e ReadExis tingDevice (Environme ntConfigur ation envi ronment) | |||||
| 388 | { | |||||
| 389 | //Retr ieve the f ile info | |||||
| 390 | FileIn fo file = GetDeviceF ile(enviro nment); | |||||
| 391 | if (!f ile.Exists ) | |||||
| 392 | { | |||||
| 393 | return n ull; | |||||
| 394 | } | |||||
| 395 | ||||||
| 396 | using (FileStrea m stream = file.Open (FileMode. Open, File Access.Rea d, FileSha re.Read)) | |||||
| 397 | { | |||||
| 398 | return D eserialize <LiveDevic e>("Loadin g Device C redentials from Disk ", stream) ; | |||||
| 399 | } | |||||
| 400 | } | |||||
| 401 | ||||||
| 402 | priv ate static void Writ eDevice(En vironmentC onfigurati on environ ment, Live Device dev ice) | |||||
| 403 | { | |||||
| 404 | FileIn fo file = GetDeviceF ile(enviro nment); | |||||
| 405 | if (!f ile.Direct ory.Exists ) | |||||
| 406 | { | |||||
| 407 | file.Dir ectory.Cre ate(); | |||||
| 408 | } | |||||
| 409 | ||||||
| 410 | using (FileStrea m stream = file.Open (FileMode. CreateNew, FileAcces s.Write, F ileShare.N one)) | |||||
| 411 | { | |||||
| 412 | Serializ e(stream, device); | |||||
| 413 | } | |||||
| 414 | } | |||||
| 415 | ||||||
| 416 | priv ate static DeviceReg istrationR esponse Ex ecuteRegis trationReq uest(strin g url, Dev iceRegistr ationReque st registr ationReque st) | |||||
| 417 | { | |||||
| 418 | //Crea te the req uest that will submi t the requ est to the server | |||||
| 419 | WebReq uest reque st = WebRe quest.Crea te(url); | |||||
| 420 | reques t.ContentT ype = "app lication/s oap+xml; c harset=UTF -8"; | |||||
| 421 | reques t.Method = "POST"; | |||||
| 422 | reques t.Timeout = 180000; | |||||
| 423 | ||||||
| 424 | //Writ e the enve lope to th e RequestS tream | |||||
| 425 | using (Stream st ream = req uest.GetRe questStrea m()) | |||||
| 426 | { | |||||
| 427 | Serializ e(stream, registrati onRequest) ; | |||||
| 428 | } | |||||
| 429 | ||||||
| 430 | // Rea d the resp onse into an XmlDocu ment and r eturn that doc | |||||
| 431 | try | |||||
| 432 | { | |||||
| 433 | using (W ebResponse response = request. GetRespons e()) | |||||
| 434 | { | |||||
| 435 | using (Str eam stream = respons e.GetRespo nseStream( )) | |||||
| 436 | { | |||||
| 437 | re turn Deser ialize<Dev iceRegistr ationRespo nse>("Dese rializing Registrati on Respons e", stream ); | |||||
| 438 | } | |||||
| 439 | } | |||||
| 440 | } | |||||
| 441 | catch (WebExcept ion ex) | |||||
| 442 | { | |||||
| 443 | System.D iagnostics .Trace.Tra ceError("M icrosoft a ccount Dev ice Regist ration Fai led (HTTP Code: {0}) : {1}", | |||||
| 444 | ex.Status, ex.Messag e); | |||||
| 445 | ||||||
| 446 | if (null != ex.Res ponse) | |||||
| 447 | { | |||||
| 448 | using (Str eam stream = ex.Resp onse.GetRe sponseStre am()) | |||||
| 449 | { | |||||
| 450 | re turn Deser ialize<Dev iceRegistr ationRespo nse>("Dese rializing Failed Reg istration Response", stream); | |||||
| 451 | } | |||||
| 452 | } | |||||
| 453 | ||||||
| 454 | throw; | |||||
| 455 | } | |||||
| 456 | } | |||||
| 457 | ||||||
| 458 | priv ate static DeviceUse rName Gene rateDevice UserName() | |||||
| 459 | { | |||||
| 460 | Device UserName u serName = new Device UserName() ; | |||||
| 461 | userNa me.DeviceN ame = Gene rateRandom String(Liv eIdConstan ts.ValidDe viceNameCh aracters, MaxDeviceN ameLength) ; | |||||
| 462 | userNa me.Decrypt edPassword = Generat eRandomStr ing(LiveId Constants. ValidDevic ePasswordC haracters, MaxDevice PasswordLe ngth); | |||||
| 463 | ||||||
| 464 | return userName; | |||||
| 465 | } | |||||
| 466 | ||||||
| 467 | priv ate static string Ge nerateRand omString(s tring char acterSet, int count) | |||||
| 468 | { | |||||
| 469 | //Crea te an arra y of the c haracters that will hold the f inal list of random characters | |||||
| 470 | char[] value = n ew char[co unt]; | |||||
| 471 | ||||||
| 472 | //Conv ert the ch aracter se t to an ar ray that c an be rand omly acces sed | |||||
| 473 | char[] set = cha racterSet. ToCharArra y(); | |||||
| 474 | ||||||
| 475 | lock ( RandomInst ance) | |||||
| 476 | { | |||||
| 477 | //Popula te the arr ay with ra ndom chara cters from the chara cter set | |||||
| 478 | for (int i = 0; i < count; i ++) | |||||
| 479 | { | |||||
| 480 | value[i] = set[Rando mInstance. Next(0, se t.Length)] ; | |||||
| 481 | } | |||||
| 482 | } | |||||
| 483 | ||||||
| 484 | return new strin g(value); | |||||
| 485 | } | |||||
| 486 | #end region | |||||
| 487 | ||||||
| 488 | #reg ion Privat e Classes | |||||
| 489 | priv ate enum E nvironment Type | |||||
| 490 | { | |||||
| 491 | LiveDe viceID, | |||||
| 492 | OrgDev iceID | |||||
| 493 | } | |||||
| 494 | ||||||
| 495 | priv ate sealed class Env ironmentCo nfiguratio n | |||||
| 496 | { | |||||
| 497 | public Environme ntConfigur ation(Envi ronmentTyp e type, st ring hostN ame, strin g environm ent) | |||||
| 498 | { | |||||
| 499 | if (stri ng.IsNullO rWhiteSpac e(hostName )) | |||||
| 500 | { | |||||
| 501 | throw new ArgumentNu llExceptio n("hostNam e"); | |||||
| 502 | } | |||||
| 503 | ||||||
| 504 | this.Typ e = type; | |||||
| 505 | this.Hos tName = ho stName; | |||||
| 506 | this.Env ironment = environme nt; | |||||
| 507 | } | |||||
| 508 | ||||||
| 509 | #regio n Properti es | |||||
| 510 | public Environme ntType Typ e { get; p rivate set ; } | |||||
| 511 | ||||||
| 512 | public string Ho stName { g et; privat e set; } | |||||
| 513 | ||||||
| 514 | public string En vironment { get; pri vate set; } | |||||
| 515 | #endre gion | |||||
| 516 | } | |||||
| 517 | ||||||
| 518 | priv ate static class Liv eIdConstan ts | |||||
| 519 | { | |||||
| 520 | public const str ing Regist rationEndp ointUriFor mat = @"ht tps://{0}/ ppsecure/D eviceAddCr edential.s rf"; | |||||
| 521 | ||||||
| 522 | public static re adonly str ing FileNa meFormat = Path.Comb ine( | |||||
| 523 | Path.Com bine(Envir onment.Get FolderPath (Environme nt.Special Folder.Use rProfile), "LiveDevi ceID"), | |||||
| 524 | "{0}{1}. xml"); | |||||
| 525 | ||||||
| 526 | public const str ing ValidD eviceNameC haracters = "0123456 789abcdefg hijklmnopq rstuvqxyz" ; | |||||
| 527 | ||||||
| 528 | //Cons ists of th e list of characters specified in the do cumentatio n | |||||
| 529 | public const str ing ValidD evicePassw ordCharact ers = "abc defghijklm nopqrstuvw xyzABCDEFG HIJKLMNOPQ RSTUVWXYZ0 123456789! @#$%^*()-_ =+;,./?`~" ; | |||||
| 530 | } | |||||
| 531 | #end region | |||||
| 532 | } | |||||
| 533 | ||||||
| 534 | #r egion Publ ic Classes & Enums | |||||
| 535 | // / <summary > | |||||
| 536 | // / Indicate s an error during re gistration | |||||
| 537 | // / </summar y> | |||||
| 538 | pu blic enum DeviceRegi strationEr rorCode | |||||
| 539 | { | |||||
| 540 | /// <summary> | |||||
| 541 | /// Unspecifie d or Unkno wn Error o ccurred | |||||
| 542 | /// </summary> | |||||
| 543 | Unkn own = 0, | |||||
| 544 | ||||||
| 545 | /// <summary> | |||||
| 546 | /// Interface Disabled | |||||
| 547 | /// </summary> | |||||
| 548 | Inte rfaceDisab led = 1, | |||||
| 549 | ||||||
| 550 | /// <summary> | |||||
| 551 | /// Invalid Re quest Form at | |||||
| 552 | /// </summary> | |||||
| 553 | Inva lidRequest Format = 3 , | |||||
| 554 | ||||||
| 555 | /// <summary> | |||||
| 556 | /// Unknown Cl ient Versi on | |||||
| 557 | /// </summary> | |||||
| 558 | Unkn ownClientV ersion = 4 , | |||||
| 559 | ||||||
| 560 | /// <summary> | |||||
| 561 | /// Blank Pass word | |||||
| 562 | /// </summary> | |||||
| 563 | Blan kPassword = 6, | |||||
| 564 | ||||||
| 565 | /// <summary> | |||||
| 566 | /// Missing De vice User Name or Pa ssword | |||||
| 567 | /// </summary> | |||||
| 568 | Miss ingDeviceU serNameOrP assword = 7, | |||||
| 569 | ||||||
| 570 | /// <summary> | |||||
| 571 | /// Invalid Pa rameter Sy ntax | |||||
| 572 | /// </summary> | |||||
| 573 | Inva lidParamet erSyntax = 8, | |||||
| 574 | ||||||
| 575 | /// <summary> | |||||
| 576 | /// Invalid Ch aracters a re used in the devic e credenti als. | |||||
| 577 | /// </summary> | |||||
| 578 | Inva lidCharact ersInCrede ntials = 9 , | |||||
| 579 | ||||||
| 580 | /// <summary> | |||||
| 581 | /// Internal E rror | |||||
| 582 | /// </summary> | |||||
| 583 | Inte rnalError = 11, | |||||
| 584 | ||||||
| 585 | /// <summary> | |||||
| 586 | /// Device Alr eady Exist s | |||||
| 587 | /// </summary> | |||||
| 588 | Devi ceAlreadyE xists = 13 | |||||
| 589 | } | |||||
| 590 | ||||||
| 591 | // / <summary > | |||||
| 592 | // / Indicate s that Dev ice Regist ration fai led | |||||
| 593 | // / </summar y> | |||||
| 594 | [S erializabl e] | |||||
| 595 | pu blic seale d class De viceRegist rationFail edExceptio n : Except ion | |||||
| 596 | { | |||||
| 597 | /// <summary> | |||||
| 598 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 599 | /// </summary> | |||||
| 600 | publ ic DeviceR egistratio nFailedExc eption() | |||||
| 601 | : base () | |||||
| 602 | { | |||||
| 603 | } | |||||
| 604 | ||||||
| 605 | /// <summary> | |||||
| 606 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 607 | /// </summary> | |||||
| 608 | /// <param nam e="message ">Message to pass</p aram> | |||||
| 609 | publ ic DeviceR egistratio nFailedExc eption(str ing messag e) | |||||
| 610 | : base (message) | |||||
| 611 | { | |||||
| 612 | } | |||||
| 613 | ||||||
| 614 | /// <summary> | |||||
| 615 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 616 | /// </summary> | |||||
| 617 | /// <param nam e="message ">Message to pass</p aram> | |||||
| 618 | /// <param nam e="innerEx ception">E xception t o include< /param> | |||||
| 619 | publ ic DeviceR egistratio nFailedExc eption(str ing messag e, Excepti on innerEx ception) | |||||
| 620 | : base (message, innerExcep tion) | |||||
| 621 | { | |||||
| 622 | } | |||||
| 623 | ||||||
| 624 | /// <summary> | |||||
| 625 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 626 | /// </summary> | |||||
| 627 | /// <param nam e="code">E rror code that occur red</param > | |||||
| 628 | /// <param nam e="subCode ">Subcode that occur red</param > | |||||
| 629 | publ ic DeviceR egistratio nFailedExc eption(Dev iceRegistr ationError Code code, string su bCode) | |||||
| 630 | : this (code, sub Code, null ) | |||||
| 631 | { | |||||
| 632 | } | |||||
| 633 | ||||||
| 634 | /// <summary> | |||||
| 635 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 636 | /// </summary> | |||||
| 637 | /// <param nam e="code">E rror code that occur red</param > | |||||
| 638 | /// <param nam e="subCode ">Subcode that occur red</param > | |||||
| 639 | /// <param nam e="innerEx ception">I nner excep tion</para m> | |||||
| 640 | publ ic DeviceR egistratio nFailedExc eption(Dev iceRegistr ationError Code code, string su bCode, Exc eption inn erExceptio n) | |||||
| 641 | : base (string.Co ncat(code. ToString() , ": ", su bCode), in nerExcepti on) | |||||
| 642 | { | |||||
| 643 | this.R egistratio nErrorCode = code; | |||||
| 644 | } | |||||
| 645 | ||||||
| 646 | /// <summary> | |||||
| 647 | /// Construct an instanc e of the D eviceRegis trationFai ledExcepti on class | |||||
| 648 | /// </summary> | |||||
| 649 | /// <param nam e="si"></p aram> | |||||
| 650 | /// <param nam e="sc"></p aram> | |||||
| 651 | priv ate Device Registrati onFailedEx ception(Se rializatio nInfo si, StreamingC ontext sc) | |||||
| 652 | : base (si, sc) | |||||
| 653 | { | |||||
| 654 | } | |||||
| 655 | ||||||
| 656 | #reg ion Proper ties | |||||
| 657 | /// <summary> | |||||
| 658 | /// Error code that occu rred durin g registra tion | |||||
| 659 | /// </summary> | |||||
| 660 | publ ic DeviceR egistratio nErrorCode Registrat ionErrorCo de { get; private se t; } | |||||
| 661 | #end region | |||||
| 662 | ||||||
| 663 | #reg ion Method s | |||||
| 664 | publ ic overrid e void Get ObjectData (Serializa tionInfo i nfo, Strea mingContex t context) | |||||
| 665 | { | |||||
| 666 | base.G etObjectDa ta(info, c ontext); | |||||
| 667 | } | |||||
| 668 | #end region | |||||
| 669 | } | |||||
| 670 | ||||||
| 671 | #r egion Seri alization Classes | |||||
| 672 | #r egion Devi ceRegistra tionReques t Class | |||||
| 673 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 674 | [X mlRoot("De viceAddReq uest")] | |||||
| 675 | pu blic seale d class De viceRegist rationRequ est | |||||
| 676 | { | |||||
| 677 | #reg ion Constr uctors | |||||
| 678 | publ ic DeviceR egistratio nRequest() | |||||
| 679 | { | |||||
| 680 | } | |||||
| 681 | ||||||
| 682 | publ ic DeviceR egistratio nRequest(G uid applic ationId, L iveDevice device) | |||||
| 683 | : this () | |||||
| 684 | { | |||||
| 685 | if (nu ll == devi ce) | |||||
| 686 | { | |||||
| 687 | throw ne w Argument NullExcept ion("devic e"); | |||||
| 688 | } | |||||
| 689 | ||||||
| 690 | this.C lientInfo = new Devi ceRegistra tionClient Info() { A pplication Id = appli cationId, Version = "1.0" }; | |||||
| 691 | this.A uthenticat ion = new DeviceRegi strationAu thenticati on() | |||||
| 692 | { | |||||
| 693 | MemberNa me = devic e.User.Dev iceId, | |||||
| 694 | Password = device. User.Decry ptedPasswo rd | |||||
| 695 | }; | |||||
| 696 | } | |||||
| 697 | #end region | |||||
| 698 | ||||||
| 699 | #reg ion Proper ties | |||||
| 700 | [Xml Element("C lientInfo" )] | |||||
| 701 | publ ic DeviceR egistratio nClientInf o ClientIn fo { get; set; } | |||||
| 702 | ||||||
| 703 | [Xml Element("A uthenticat ion")] | |||||
| 704 | publ ic DeviceR egistratio nAuthentic ation Auth entication { get; se t; } | |||||
| 705 | #end region | |||||
| 706 | } | |||||
| 707 | #e ndregion | |||||
| 708 | ||||||
| 709 | #r egion Devi ceRegistra tionClient Info Class | |||||
| 710 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 711 | [X mlRoot("Cl ientInfo") ] | |||||
| 712 | pu blic seale d class De viceRegist rationClie ntInfo | |||||
| 713 | { | |||||
| 714 | #reg ion Proper ties | |||||
| 715 | [Xml Attribute( "name")] | |||||
| 716 | publ ic Guid Ap plicationI d { get; s et; } | |||||
| 717 | ||||||
| 718 | [Xml Attribute( "version") ] | |||||
| 719 | publ ic string Version { get; set; } | |||||
| 720 | #end region | |||||
| 721 | } | |||||
| 722 | #e ndregion | |||||
| 723 | ||||||
| 724 | #r egion Devi ceRegistra tionAuthen tication C lass | |||||
| 725 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 726 | [X mlRoot("Au thenticati on")] | |||||
| 727 | pu blic seale d class De viceRegist rationAuth entication | |||||
| 728 | { | |||||
| 729 | #reg ion Proper ties | |||||
| 730 | [Xml Element("M embername" )] | |||||
| 731 | publ ic string MemberName { get; se t; } | |||||
| 732 | ||||||
| 733 | [Xml Element("P assword")] | |||||
| 734 | publ ic string Password { get; set; } | |||||
| 735 | #end region | |||||
| 736 | } | |||||
| 737 | #e ndregion | |||||
| 738 | ||||||
| 739 | #r egion Devi ceRegistra tionRespon se Class | |||||
| 740 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 741 | [X mlRoot("De viceAddRes ponse")] | |||||
| 742 | pu blic seale d class De viceRegist rationResp onse | |||||
| 743 | { | |||||
| 744 | #reg ion Proper ties | |||||
| 745 | [Xml Element("s uccess")] | |||||
| 746 | publ ic bool Is Success { get; set; } | |||||
| 747 | ||||||
| 748 | [Xml Element("p uid")] | |||||
| 749 | publ ic string Puid { get ; set; } | |||||
| 750 | ||||||
| 751 | [Xml Element("E rror")] | |||||
| 752 | publ ic DeviceR egistratio nResponseE rror Error { get; se t; } | |||||
| 753 | ||||||
| 754 | [Xml Element("E rrorSubcod e")] | |||||
| 755 | publ ic string ErrorSubCo de { get; set; } | |||||
| 756 | #end region | |||||
| 757 | } | |||||
| 758 | #e ndregion | |||||
| 759 | ||||||
| 760 | #r egion Devi ceRegistra tionRespon se Class | |||||
| 761 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 762 | [X mlRoot("Er ror")] | |||||
| 763 | pu blic seale d class De viceRegist rationResp onseError | |||||
| 764 | { | |||||
| 765 | priv ate string _code; | |||||
| 766 | ||||||
| 767 | #reg ion Proper ties | |||||
| 768 | [Xml Attribute( "Code")] | |||||
| 769 | publ ic string Code | |||||
| 770 | { | |||||
| 771 | get | |||||
| 772 | { | |||||
| 773 | return t his._code; | |||||
| 774 | } | |||||
| 775 | ||||||
| 776 | set | |||||
| 777 | { | |||||
| 778 | this._co de = value ; | |||||
| 779 | ||||||
| 780 | //Parse the error code | |||||
| 781 | if (!str ing.IsNull OrEmpty(va lue)) | |||||
| 782 | { | |||||
| 783 | //Parse th e error co de | |||||
| 784 | if (value. StartsWith ("dc", Str ingCompari son.Ordina l)) | |||||
| 785 | { | |||||
| 786 | in t code; | |||||
| 787 | if (int.TryP arse(value .Substring (2), Numbe rStyles.In teger, | |||||
| 788 | Cult ureInfo.In variantCul ture, out code) && | |||||
| 789 | Enum .IsDefined (typeof(De viceRegist rationErro rCode), co de)) | |||||
| 790 | { | |||||
| 791 | this .Registrat ionErrorCo de = (Devi ceRegistra tionErrorC ode)Enum.T oObject( | |||||
| 792 | typeof (DeviceReg istrationE rrorCode), code); | |||||
| 793 | } | |||||
| 794 | } | |||||
| 795 | } | |||||
| 796 | } | |||||
| 797 | } | |||||
| 798 | ||||||
| 799 | [Xml Ignore] | |||||
| 800 | publ ic DeviceR egistratio nErrorCode Registrat ionErrorCo de { get; private se t; } | |||||
| 801 | #end region | |||||
| 802 | } | |||||
| 803 | #e ndregion | |||||
| 804 | ||||||
| 805 | #r egion Live Device Cla ss | |||||
| 806 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 807 | [X mlRoot("Da ta")] | |||||
| 808 | pu blic seale d class Li veDevice | |||||
| 809 | { | |||||
| 810 | #reg ion Proper ties | |||||
| 811 | [Xml Attribute( "version") ] | |||||
| 812 | publ ic int Ver sion { get ; set; } | |||||
| 813 | ||||||
| 814 | [Xml Element("U ser")] | |||||
| 815 | publ ic DeviceU serName Us er { get; set; } | |||||
| 816 | ||||||
| 817 | [Sup pressMessa ge("Micros oft.Design ", "CA1059 :MembersSh ouldNotExp oseCertain ConcreteTy pes", Mess ageId = "S ystem.Xml. XmlNode", Justificat ion = "Thi s is requi red for pr oper XML S erializati on")] | |||||
| 818 | [Xml Element("T oken")] | |||||
| 819 | publ ic XmlNode Token { g et; set; } | |||||
| 820 | ||||||
| 821 | [Xml Element("E xpiry")] | |||||
| 822 | publ ic string Expiry { g et; set; } | |||||
| 823 | ||||||
| 824 | [Xml Element("C lockSkew") ] | |||||
| 825 | publ ic string ClockSkew { get; set ; } | |||||
| 826 | #end region | |||||
| 827 | } | |||||
| 828 | #e ndregion | |||||
| 829 | ||||||
| 830 | #r egion Devi ceUserName Class | |||||
| 831 | [E ditorBrows able(Edito rBrowsable State.Neve r)] | |||||
| 832 | pu blic seale d class De viceUserNa me | |||||
| 833 | { | |||||
| 834 | priv ate string _encrypte dPassword; | |||||
| 835 | priv ate string _decrypte dPassword; | |||||
| 836 | priv ate bool _ encryptedV alueIsUpda ted; | |||||
| 837 | ||||||
| 838 | #reg ion Consta nts | |||||
| 839 | priv ate const string Use rNamePrefi x = "11"; | |||||
| 840 | #end region | |||||
| 841 | ||||||
| 842 | #reg ion Constr uctors | |||||
| 843 | publ ic DeviceU serName() | |||||
| 844 | { | |||||
| 845 | this.U serNameTyp e = "Logic al"; | |||||
| 846 | } | |||||
| 847 | #end region | |||||
| 848 | ||||||
| 849 | #reg ion Proper ties | |||||
| 850 | [Xml Attribute( "username" )] | |||||
| 851 | publ ic string DeviceName { get; se t; } | |||||
| 852 | ||||||
| 853 | [Xml Attribute( "type")] | |||||
| 854 | publ ic string UserNameTy pe { get; set; } | |||||
| 855 | ||||||
| 856 | [Xml Element("P wd")] | |||||
| 857 | publ ic string EncryptedP assword | |||||
| 858 | { | |||||
| 859 | get | |||||
| 860 | { | |||||
| 861 | this.Thr owIfNoEncr yption(); | |||||
| 862 | ||||||
| 863 | if (!thi s._encrypt edValueIsU pdated) | |||||
| 864 | { | |||||
| 865 | this._encr yptedPassw ord = this .Encrypt(t his._decry ptedPasswo rd); | |||||
| 866 | this._encr yptedValue IsUpdated = true; | |||||
| 867 | } | |||||
| 868 | ||||||
| 869 | return t his._encry ptedPasswo rd; | |||||
| 870 | } | |||||
| 871 | ||||||
| 872 | set | |||||
| 873 | { | |||||
| 874 | this.Thr owIfNoEncr yption(); | |||||
| 875 | this.Upd ateCredent ials(value , null); | |||||
| 876 | } | |||||
| 877 | } | |||||
| 878 | ||||||
| 879 | publ ic string DeviceId | |||||
| 880 | { | |||||
| 881 | get | |||||
| 882 | { | |||||
| 883 | return U serNamePre fix + Devi ceName; | |||||
| 884 | } | |||||
| 885 | } | |||||
| 886 | ||||||
| 887 | [Xml Ignore] | |||||
| 888 | publ ic string DecryptedP assword | |||||
| 889 | { | |||||
| 890 | get | |||||
| 891 | { | |||||
| 892 | return t his._decry ptedPasswo rd; | |||||
| 893 | } | |||||
| 894 | ||||||
| 895 | set | |||||
| 896 | { | |||||
| 897 | this.Upd ateCredent ials(null, value); | |||||
| 898 | } | |||||
| 899 | } | |||||
| 900 | ||||||
| 901 | priv ate bool I sEncryptio nEnabled | |||||
| 902 | { | |||||
| 903 | get | |||||
| 904 | { | |||||
| 905 | //If the object is not going to be per sisted to a file, th en the val ue does no t need to be encrypt ed. This i s extra | |||||
| 906 | //overhe ad and wil l not func tion in pa rtial trus t. | |||||
| 907 | return D eviceIdMan ager.Persi stToFile; | |||||
| 908 | } | |||||
| 909 | } | |||||
| 910 | #end region | |||||
| 911 | ||||||
| 912 | #reg ion Method s | |||||
| 913 | publ ic ClientC redentials ToClientC redentials () | |||||
| 914 | { | |||||
| 915 | Client Credential s credenti als = new ClientCred entials(); | |||||
| 916 | creden tials.User Name.UserN ame = this .DeviceId; | |||||
| 917 | creden tials.User Name.Passw ord = this .Decrypted Password; | |||||
| 918 | ||||||
| 919 | return credentia ls; | |||||
| 920 | } | |||||
| 921 | ||||||
| 922 | priv ate void T hrowIfNoEn cryption() | |||||
| 923 | { | |||||
| 924 | if (!t his.IsEncr yptionEnab led) | |||||
| 925 | { | |||||
| 926 | throw ne w NotSuppo rtedExcept ion("Not s upported w hen Device IdManager. UseEncrypt ionApis is false."); | |||||
| 927 | } | |||||
| 928 | } | |||||
| 929 | ||||||
| 930 | priv ate void U pdateCrede ntials(str ing encryp tedValue, string dec ryptedValu e) | |||||
| 931 | { | |||||
| 932 | bool i sValueUpda ted = fals e; | |||||
| 933 | if (st ring.IsNul lOrEmpty(e ncryptedVa lue) && st ring.IsNul lOrEmpty(d ecryptedVa lue)) | |||||
| 934 | { | |||||
| 935 | isValueU pdated = t rue; | |||||
| 936 | } | |||||
| 937 | else i f (string. IsNullOrEm pty(encryp tedValue)) | |||||
| 938 | { | |||||
| 939 | if (this .IsEncrypt ionEnabled ) | |||||
| 940 | { | |||||
| 941 | encryptedV alue = thi s.Encrypt( decryptedV alue); | |||||
| 942 | isValueUpd ated = tru e; | |||||
| 943 | } | |||||
| 944 | else | |||||
| 945 | { | |||||
| 946 | encryptedV alue = nul l; | |||||
| 947 | isValueUpd ated = fal se; | |||||
| 948 | } | |||||
| 949 | } | |||||
| 950 | else | |||||
| 951 | { | |||||
| 952 | this.Thr owIfNoEncr yption(); | |||||
| 953 | ||||||
| 954 | decrypte dValue = t his.Decryp t(encrypte dValue); | |||||
| 955 | isValueU pdated = t rue; | |||||
| 956 | } | |||||
| 957 | ||||||
| 958 | this._ encryptedP assword = encryptedV alue; | |||||
| 959 | this._ decryptedP assword = decryptedV alue; | |||||
| 960 | this._ encryptedV alueIsUpda ted = isVa lueUpdated ; | |||||
| 961 | } | |||||
| 962 | ||||||
| 963 | priv ate string Encrypt(s tring valu e) | |||||
| 964 | { | |||||
| 965 | if (st ring.IsNul lOrEmpty(v alue)) | |||||
| 966 | { | |||||
| 967 | return v alue; | |||||
| 968 | } | |||||
| 969 | ||||||
| 970 | byte[] encrypted Bytes = Pr otectedDat a.Protect( Encoding.U TF8.GetByt es(value), null, Dat aProtectio nScope.Cur rentUser); | |||||
| 971 | return Convert.T oBase64Str ing(encryp tedBytes); | |||||
| 972 | } | |||||
| 973 | ||||||
| 974 | priv ate string Decrypt(s tring valu e) | |||||
| 975 | { | |||||
| 976 | if (st ring.IsNul lOrEmpty(v alue)) | |||||
| 977 | { | |||||
| 978 | return v alue; | |||||
| 979 | } | |||||
| 980 | ||||||
| 981 | byte[] decrypted Bytes = Pr otectedDat a.Unprotec t(Convert. FromBase64 String(val ue), null, DataProte ctionScope .CurrentUs er); | |||||
| 982 | if (nu ll == decr yptedBytes || 0 == d ecryptedBy tes.Length ) | |||||
| 983 | { | |||||
| 984 | return n ull; | |||||
| 985 | } | |||||
| 986 | ||||||
| 987 | return Encoding. UTF8.GetSt ring(decry ptedBytes, 0, decryp tedBytes.L ength); | |||||
| 988 | } | |||||
| 989 | #end region | |||||
| 990 | } | |||||
| 991 | #e ndregion | |||||
| 992 | #e ndregion | |||||
| 993 | #e ndregion | |||||
| 994 | } | |||||
| 995 | //</snippe tDeviceIdM anager> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.