Produced by Araxis Merge on 11/29/2018 1:28:46 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 | PPMS Build 9.zip\CIF B10S1\VA.PPMS.Plugins\VA.PPMS.Plugins\VA.PPMS.CRM.Plugins\Helper | AddressValidationHelper.cs | Tue Nov 20 19:11:50 2018 UTC |
| 2 | PPMS Build 9.zip\CIF B10S1\VA.PPMS.Plugins\VA.PPMS.Plugins\VA.PPMS.CRM.Plugins\Helper | AddressValidationHelper.cs | Wed Nov 28 17:06:52 2018 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 8 | 1098 |
| Changed | 7 | 18 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| 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 | using Syst em; | |
| 2 | using Syst em.Collect ions.Gener ic; | |
| 3 | using Syst em.IO; | |
| 4 | using Syst em.Net; | |
| 5 | using Syst em.Net.Htt p; | |
| 6 | using Syst em.Net.Htt p.Headers; | |
| 7 | using Syst em.Web; | |
| 8 | using Syst em.Runtime .Serializa tion.Json; | |
| 9 | using Syst em.Text.Re gularExpre ssions; | |
| 10 | using Syst em.Securit y.Cryptogr aphy.X509C ertificate s; | |
| 11 | using Syst em.Text; | |
| 12 | using Micr osoft.Xrm. Sdk; | |
| 13 | using VA.P PMS.Contex t; | |
| 14 | using Syst em.Linq; | |
| 15 | ||
| 16 | namespace VA.PPMS.CR M.Plugins. Helper | |
| 17 | { | |
| 18 | ||
| 19 | public static cl ass Addres sValidatio nHelper | |
| 20 | { | |
| 21 | pu blic stati c ITracing Service tr acingServi ce; | |
| 22 | ||
| 23 | pu blic stati c RootObje ctResponse ValidateW cIWS(Entit y careSite , string s treetAddre ss, string city, str ing state, string zi p, ITracin gService _ tracingSer vice, Ppms Context sv c, IOrgani zationServ ice _servi ce) | |
| 24 | { | |
| 25 | ||
| 26 | tracingS ervice = _ tracingSer vice; | |
| 27 | // Get o rganizatio n service reference | |
| 28 | tracingS ervice.Tra ce("Beginn ing Addres s Validati on"); | |
| 29 | tracingS ervice.Tra ce("Addres s Attribut es for Req uest:" + s treetAddre ss + ", " + city + " , " + stat e + ", " + zip); | |
| 30 | ||
| 31 | var addr essValidat ionResult = new Root ObjectResp onse(); | |
| 32 | ||
| 33 | using (W ebClient w c = new We bClient()) | |
| 34 | { | |
| 35 | //string p roxyAddres sString = "https:// DNS . URL /ase/Valid ateAddress /v1?street Address="; | |
| 36 | ||
| 37 | //Re trieve Add ress Valid ation URL from Setti ngs Entity | |
| 38 | stri ng proxyAd dressStrin g = ""; | |
| 39 | var settings = svc.ppms_ integratio nsettingsS et.FirstOr Default(i => i.Id != null); | |
| 40 | if ( settings = = null) { tracingSer vice.Trace ("Unable t o locate C RM Setting s entity") ; return n ull; } | |
| 41 | if ( settings.p pms_Addres sValidatio nEndpoint == String. Empty || s ettings.pp ms_Address Validation Endpoint = = null) { tracingSer vice.Trace ("Unable t o locate A ddress Val idation En dpoint URL on Integr ation Sett ings Recor d"); retur n null; } | |
| 42 | else | |
| 43 | { | |
| 44 | tracingSer vice.Trace ("Address Validation URL Locat ed on CRM Settings e ntity"); | |
| 45 | proxyAddre ssString = settings. ppms_Addre ssValidati onEndpoint ; | |
| 46 | } | |
| 47 | ||
| 48 | prox yAddressSt ring += "' " + street Address + "'"; | |
| 49 | prox yAddressSt ring += "& city="; | |
| 50 | prox yAddressSt ring += ci ty; | |
| 51 | prox yAddressSt ring += "& state="; | |
| 52 | prox yAddressSt ring += st ate; | |
| 53 | prox yAddressSt ring += "& zip="; | |
| 54 | prox yAddressSt ring += zi p; | |
| 55 | prox yAddressSt ring = Uri .EscapeUri String(pro xyAddressS tring).Rep lace("#", "%23"); | |
| 56 | ||
| 57 | trac ingService .Trace("Ad dress Stri ng: " + pr oxyAddress String); | |
| 58 | ||
| 59 | wc.H eaders[Htt pRequestHe ader.Conte ntType] = "applicati on/json"; | |
| 60 | ||
| 61 | ||
| 62 | trac ingService .Trace("Ma king Call to IWS Add ress Valid ation Func tion"); | |
| 63 | stri ng iwsResp onse = wc. DownloadSt ring(proxy AddressStr ing); | |
| 64 | ||
| 65 | if ( !string.Is NullOrEmpt y(iwsRespo nse)) | |
| 66 | { | |
| 67 | addressVal idationRes ult = Dese rialize<Ro otObjectRe sponse>(iw sResponse) ; | |
| 68 | if (addres sValidatio nResult != null) | |
| 69 | { | |
| 70 | tracin gService.T race("Suce ssful Resp onse from Address Va lidation A PI"); | |
| 71 | tracin gService.T race("Conf idence Sco re:" + add ressValida tionResult .addressMe taData.con fidenceSco re); | |
| 72 | } | |
| 73 | else | |
| 74 | { | |
| 75 | tracin gService.T race("Unsu ccessful R esponse fr om Address Validaton API"); | |
| 76 | return null; | |
| 77 | } | |
| 78 | return add ressValida tionResult ; | |
| 79 | } | |
| 80 | else | |
| 81 | { | |
| 82 | tracingSer vice.Trace ("Invalid String ret urned"); | |
| 83 | return nul l; | |
| 84 | } | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | ||
| 89 | pu blic stati c T Deseri alize<T>(s tring json ) | |
| 90 | { | |
| 91 | var clea nJson = Va lidateJson (json); | |
| 92 | var ms = new Memor yStream(En coding.UTF 8.GetBytes (cleanJson )); | |
| 93 | var ser = new Data ContractJs onSerializ er(typeof( T)); | |
| 94 | var resu lt = (T)se r.ReadObje ct(ms); | |
| 95 | ms.Close (); | |
| 96 | return r esult; | |
| 97 | } | |
| 98 | ||
| 99 | pr ivate stat ic string ValidateJs on(string json) | |
| 100 | { | |
| 101 | //Checki ng the Res ponse Stri ng to make sure not null or ma tches comm on XSS | |
| 102 | var Pote ntialXssAt tackExpres sion = "(h ttp(s)*(%3 a|:))|(ftp (s)*(%3a|: ))|(javasc ript)|(ale rt)|(((\\% 3C) <)[^\n ]+((\\%3E) >))"; | |
| 103 | Regex Po tentialXss AttackRege x = new Re gex(Potent ialXssAtta ckExpressi on, RegexO ptions.Ign oreCase); | |
| 104 | if (Pote ntialXssAt tackRegex. IsMatch(js on)) | |
| 105 | { | |
| 106 | thro w new Inva lidDataExc eption("In put is inv alid"); | |
| 107 | } | |
| 108 | else | |
| 109 | { | |
| 110 | retu rn json; | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | ||
| 115 | /* | |
| 116 | pu blic stati c string S erializee< T>(T data) | |
| 117 | { | |
| 118 | var ms = new Memor yStream(); | |
| 119 | var ser = new Data ContractJs onSerializ er(typeof( T)); | |
| 120 | ser.Writ eObject(ms , data); | |
| 121 | var json = ms.ToAr ray(); | |
| 122 | ms.Close (); | |
| 123 | ||
| 124 | return E ncoding.UT F8.GetStri ng(json, 0 , json.Len gth); | |
| 125 | } | |
| 126 | ||
| 127 | pu blic stati c AddressD ata Valida teWcDWS(st ring stree tAddress, string cit y, string state, str ing zip, I TracingSer vice _trac ingService ) | |
| 128 | { | |
| 129 | try | |
| 130 | { | |
| 131 | trac ingService = _tracin gService; | |
| 132 | trac ingService .Trace("Be ginning Ad dress Vali dation"); | |
| 133 | trac ingService .Trace("Ad dress Attr ibues for Request:" + streetAd dress + ", " + city + ", " + s tate + ", " + zip); | |
| 134 | ||
| 135 | var addressDat a = new Ad dressData( ); | |
| 136 | ||
| 137 | usin g (WebClie nt wc = ne w WebClien t()) | |
| 138 | { | |
| 139 | //string a ddressStri ng = "http ://dev. DNS . URL /v1.0/Vali dateAddres s?streetAd dress="; | |
| 140 | string pro xyAddressS tring = "h ttps:// DNS . URL /ase/v1.0/ ValidateAd dress?stre etAddress= "; | |
| 141 | ||
| 142 | proxyAddre ssString + = "'" + st reetAddres s + "'"; | |
| 143 | proxyAddre ssString + = "&city=" ; | |
| 144 | proxyAddre ssString + = city; | |
| 145 | proxyAddre ssString + = "&state= "; | |
| 146 | proxyAddre ssString + = state; | |
| 147 | proxyAddre ssString + = "&zip="; | |
| 148 | proxyAddre ssString + = zip; | |
| 149 | proxyAddre ssString = Uri.Escap eUriString (proxyAddr essString) .Replace(" #", "%23") ; | |
| 150 | ||
| 151 | //var test String = " https://de v. DNS . URL /v1.0/Vali dateAddres s?streetAd dress="; | |
| 152 | ||
| 153 | tracingSer vice.Trace ("Address String: " + proxyAdd ressString ); | |
| 154 | ||
| 155 | wc.Headers [HttpReque stHeader.C ontentType ] = "appli cation/jso n"; | |
| 156 | ||
| 157 | tracingSer vice.Trace ("Making C all to DWS Address V alidation Function") ; | |
| 158 | string dws Response = wc.Downlo adString(p roxyAddres sString); | |
| 159 | AddressDat aValues ad dressDataV alues = De serialize< AddressDat aValues>(d wsResponse ); | |
| 160 | if (addres sDataValue s.value.Co unt > 0) | |
| 161 | { | |
| 162 | tracin gService.T race("Suce ssful Resp onse from Address Va lidation A PI"); | |
| 163 | addres sData = ad dressDataV alues.valu e[0]; | |
| 164 | _traci ngService. Trace("Con fidence Sc ore:" + ad dressData. Confidence Score); | |
| 165 | } | |
| 166 | else | |
| 167 | { | |
| 168 | tracin gService.T race("Unsu ccessful R esponse fr om Address Validatio n API"); | |
| 169 | } | |
| 170 | return add ressData; | |
| 171 | } | |
| 172 | } | |
| 173 | catch (W ebExceptio n we) | |
| 174 | { | |
| 175 | trac ingService .Trace("We b Exceptio n on call to Address Validatio n Service: ", we.Mess age.ToStri ng()); | |
| 176 | retu rn null; | |
| 177 | } | |
| 178 | catch (E xception e ) | |
| 179 | { | |
| 180 | trac ingService .Trace("Ex ception on call to A ddress Val idation Se rvice:", e .ToString( )); | |
| 181 | retu rn null; | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | ||
| 186 | ||
| 187 | pu blic stati c RootObje ctResponse ValidateW C(string s treetAddre ss, string city, str ing state, string zi p, ITracin gService _ tracingSer vice) | |
| 188 | { | |
| 189 | try | |
| 190 | { | |
| 191 | trac ingService = _tracin gService; | |
| 192 | ||
| 193 | trac ingService .Trace("Be ginning Ad dress Vali dation"); | |
| 194 | ||
| 195 | trac ingService .Trace("Ad dress Attr ibues for Request:" + streetAd dress + ", " + city + ", " + s tate + ", " + zip); | |
| 196 | var addressDat a = new Ro otObjectRe sponse(); | |
| 197 | ||
| 198 | usin g (MyClien t client = new MyCli ent()) | |
| 199 | { | |
| 200 | client.Hea ders[HttpR equestHead er.Content Type] = "a pplication /json"; | |
| 201 | ||
| 202 | var payloa d = new Ro otObjectRe quest | |
| 203 | { | |
| 204 | reques tAddress = new Addre ss | |
| 205 | { | |
| 206 | ad dressLine1 = streetA ddress, | |
| 207 | ci ty = city, | |
| 208 | st ateProvinc e = new St ateProvinc e | |
| 209 | { | |
| 210 | name = s tate | |
| 211 | }, | |
| 212 | zi pCode5 = z ip | |
| 213 | } | |
| 214 | }; | |
| 215 | var json = Serialize e(payload) ; | |
| 216 | ||
| 217 | tracingSer vice.Trace ("Making C all to Add ress Valid ation Serv ivce"); | |
| 218 | string res ponse = cl ient.Uploa dString(ne w Uri("htt ps:// DNS . URL /address-v alidation/ address/v1 /validate" ), json); | |
| 219 | addressDat a = Deseri alize<Root ObjectResp onse>(resp onse); | |
| 220 | if (addres sData != n ull && add ressData.a ddressMeta Data.deliv eryPointVa lidation = = "CONFIRM ED") | |
| 221 | { | |
| 222 | tracin gService.T race("Suce ssful Resp onse from Address Va lidation A PI"); | |
| 223 | } | |
| 224 | tracingSer vice.Trace ("Unsucces sful Respo nse from A ddress Val idation AP I"); | |
| 225 | } | |
| 226 | retu rn address Data; | |
| 227 | } | |
| 228 | catch (W ebExceptio n we) | |
| 229 | { | |
| 230 | trac ingService .Trace("We b Exceptio n on call to Address Validatio n Service: ", we.Mess age.ToStri ng()); | |
| 231 | thro w; | |
| 232 | } | |
| 233 | catch (E xception e ) | |
| 234 | { | |
| 235 | trac ingService .Trace("Ex ception on call to A ddress Val idation Se rvice:", e .ToString( )); | |
| 236 | retu rn null; | |
| 237 | } | |
| 238 | } | |
| 239 | ||
| 240 | ||
| 241 | ||
| 242 | pu blic stati c AddressD ata Valida teHttp(str ing street Address, s tring city , string s tate, stri ng zip, IT racingServ ice _traci ngService) | |
| 243 | { | |
| 244 | try | |
| 245 | { | |
| 246 | trac ingService = _tracin gService; | |
| 247 | trac ingService .Trace("Be ginning Ad dress Vali dation"); | |
| 248 | trac ingService .Trace("Ad dress Attr ibues for Request:" + streetAd dress + ", " + city + ", " + s tate + ", " + zip); | |
| 249 | ||
| 250 | var addressDat a = new Ad dressData( ); | |
| 251 | ||
| 252 | usin g (HttpCli ent client = new Htt pClient()) | |
| 253 | { | |
| 254 | //string a ddressStri ng = "http s://dev. DNS . URL /v1.0/Vali dateAddres s?streetAd dress="; | |
| 255 | string pro xyAddressS tring = "h ttps:// DNS . URL /ase/v1.0/ ValidateAd dress?stre etAddress= "; | |
| 256 | ||
| 257 | proxyAddre ssString + = "'1239 V ermont Ave NW #605'" ; | |
| 258 | proxyAddre ssString + = "&city=" ; | |
| 259 | proxyAddre ssString + = "Washing ton"; | |
| 260 | proxyAddre ssString + = "&state= "; | |
| 261 | proxyAddre ssString + = "DC"; | |
| 262 | proxyAddre ssString + = "&zip="; | |
| 263 | proxyAddre ssString + = "20005"; | |
| 264 | proxyAddre ssString = Uri.Escap eUriString (proxyAddr essString) .Replace(" #", "%23") ; | |
| 265 | ||
| 266 | tracingSer vice.Trace ("Making C all to DWS Address V alidation Function") ; | |
| 267 | ||
| 268 | var respon se = clien t.GetStrin gAsync(pro xyAddressS tring).Get Awaiter(). GetResult( ); | |
| 269 | ||
| 270 | AddressDat aValues ad dressDataV alues = De serialize< AddressDat aValues>(r esponse); | |
| 271 | if (addres sDataValue s.value.Co unt > 0) | |
| 272 | { | |
| 273 | tracin gService.T race("Suce ssful Resp onse from Address Va lidation A PI"); | |
| 274 | addres sData = ad dressDataV alues.valu e[0]; | |
| 275 | } | |
| 276 | } | |
| 277 | trac ingService .Trace("Un successful Response from Addre ss Validat ion API"); | |
| 278 | retu rn address Data; | |
| 279 | } | |
| 280 | catch (W ebExceptio n we) | |
| 281 | { | |
| 282 | trac ingService .Trace("We b Exceptio n on call to Address Validatio n Service: ", we.Mess age.ToStri ng()); | |
| 283 | thro w; | |
| 284 | } | |
| 285 | catch (E xception e ) | |
| 286 | { | |
| 287 | trac ingService .Trace("Ex ception on call to A ddress Val idation Se rvice:", e .ToString( )); | |
| 288 | thro w; | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | pu blic stati c RootObje ctResponse Validate( string str eetAddress , string c ity, strin g state, s tring zip, Microsoft .Xrm.Sdk.I TracingSer vice traci ngService) | |
| 293 | { | |
| 294 | try | |
| 295 | { | |
| 296 | trac ingService .Trace("Be ginning Ad dress Vali dation"); | |
| 297 | //Se rvicePoint Manager.Se rverCertif icateValid ationCallb ack += (se nder, cert ificate, c hain, sslP olicyError s) => true ; | |
| 298 | //Se rvicePoint Manager.Se curityProt ocol = Sec urityProto colType.Tl s12; | |
| 299 | //Se rvicePoint Manager.Se curityProt ocol = Sec urityProto colType.Ss l3 | Secur ityProtoco lType.Tls | Security ProtocolTy pe.Tls11 | SecurityP rotocolTyp e.Tls12; | |
| 300 | ||
| 301 | trac ingService .Trace("Ad dress Attr ibues for Request:" + streetAd dress + ", " + city + ", " + s tate + ", " + zip); | |
| 302 | var addressDat a = new Ro otObjectRe sponse(); | |
| 303 | ||
| 304 | usin g (var cli ent = GetH ttpClient( tracingSer vice)) | |
| 305 | { | |
| 306 | tracingSer vice.Trace ("Setting Client Bas e Address" ); | |
| 307 | ||
| 308 | ||
| 309 | ||
| 310 | client.Bas eAddress = new Uri(" https:// DNS . URL "); | |
| 311 | var payloa d = new Ro otObjectRe quest | |
| 312 | { | |
| 313 | reques tAddress = new Addre ss | |
| 314 | { | |
| 315 | ad dressLine1 = streetA ddress, | |
| 316 | ci ty = city, | |
| 317 | st ateProvinc e = new St ateProvinc e | |
| 318 | { | |
| 319 | name = s tate | |
| 320 | }, | |
| 321 | zi pCode5 = z ip | |
| 322 | } | |
| 323 | }; | |
| 324 | ||
| 325 | var json = Serialize e(payload) ; | |
| 326 | ||
| 327 | var conten t = new St ringConten t(json, En coding.UTF 8, "applic ation/json "); | |
| 328 | content.He aders.Cont entType = new MediaT ypeHeaderV alue("appl ication/js on"); | |
| 329 | ||
| 330 | tracingSer vice.Trace ("Making C all to Add ress Valid ation Serv ivce"); | |
| 331 | var respon se = clien t.PostAsyn c("/addres s-validati on/address /v1/valida te", conte nt).GetAwa iter().Get Result(); | |
| 332 | ||
| 333 | if (respon se.IsSucce ssStatusCo de) | |
| 334 | { | |
| 335 | tracin gService.T race("Suce ssful Resp onse from Address Va lidation A PI"); | |
| 336 | var re sult = res ponse.Cont ent.ReadAs StringAsyn c().GetAwa iter().Get Result(); | |
| 337 | if (!s tring.IsNu llOrEmpty( result)) | |
| 338 | { | |
| 339 | ad dressData = Deserial ize<RootOb jectRespon se>(result ); | |
| 340 | } | |
| 341 | } | |
| 342 | tracingSer vice.Trace ("Unsucces sful Respo nse from A ddress Val idation AP I"); | |
| 343 | } | |
| 344 | retu rn address Data; | |
| 345 | } | |
| 346 | catch (H ttpRequest Exception he) | |
| 347 | { | |
| 348 | trac ingService .Trace("Ht tp Web Exc eption on call to Ad dress Vali dation Ser vice:", he .ToString( )); | |
| 349 | retu rn null; | |
| 350 | } | |
| 351 | catch (E xception e ) | |
| 352 | { | |
| 353 | trac ingService .Trace("Ex ception on call to A ddress Val idation Se rvice:", e .ToString( )); | |
| 354 | thro w; | |
| 355 | } | |
| 356 | } | |
| 357 | ||
| 358 | pr ivate stat ic HttpCli ent GetHtt pClient(IT racingServ ice tracin gService) | |
| 359 | { | |
| 360 | try | |
| 361 | { | |
| 362 | trac ingService .Trace("Ge tting Http Client"); | |
| 363 | var clientHand ler = new WebRequest Handler(); | |
| 364 | clie ntHandler. ClientCert ificates.A dd(GetCert KeyVault(t racingServ ice)); | |
| 365 | //cl ientHandle r.ClientCe rtificates .Add(GetLo calCert()) ; | |
| 366 | trac ingService .Trace("Cl ient Handl er Establi shed"); | |
| 367 | retu rn new Htt pClient(cl ientHandle r); | |
| 368 | } | |
| 369 | catch(We bException we) | |
| 370 | { | |
| 371 | trac ingService .Trace("Cl ient Handl er Web Exc eption"); | |
| 372 | thro w; | |
| 373 | } | |
| 374 | catch(Ex ception e) | |
| 375 | { | |
| 376 | trac ingService .Trace("Cl ient Handl er Excepti on"); | |
| 377 | thro w; | |
| 378 | } | |
| 379 | } | |
| 380 | ||
| 381 | ||
| 382 | cl ass MyClie nt : WebCl ient | |
| 383 | { | |
| 384 | protecte d override WebReques t GetWebRe quest(Uri address) | |
| 385 | { | |
| 386 | Http WebRequest request = (HttpWebR equest)bas e.GetWebRe quest(addr ess); | |
| 387 | requ est.Client Certificat es.Add(Get CertKeyVau lt(tracing Service)); | |
| 388 | retu rn request ; | |
| 389 | } | |
| 390 | } | |
| 391 | ||
| 392 | pu blic stati c X509Cert ificate2 G etLocalCer t() | |
| 393 | { | |
| 394 | var stor e = new X5 09Store(St oreName.My , StoreLoc ation.Loca lMachine); | |
| 395 | store.Op en(OpenFla gs.OpenExi stingOnly | OpenFlag s.ReadOnly ); | |
| 396 | string cer tificateSu bjectName = "CN= DNS . URL , OU=PPMS, O=VA, L=W ashington, S=DC, C=U S"; | |
| 397 | var cert = store.C ertificate s.Find(X50 9FindType. FindBySubj ectDisting uishedName , certific ateSubject Name, | |
| 398 | true ); | |
| 399 | if (cert .Count < 1 ) | |
| 400 | { | |
| 401 | thro w new Exce ption(stri ng.Format( "Could not find a va lid client certifica te with su bject {0}" , | |
| 402 | certificat eSubjectNa me)); | |
| 403 | } | |
| 404 | ||
| 405 | return c ert[0]; | |
| 406 | } | |
| 407 | ||
| 408 | pr ivate stat ic X509Cer tificate2 GetCertKey Vault(Micr osoft.Xrm. Sdk.ITraci ngService tracingSer vice) | |
| 409 | { | |
| 410 | try | |
| 411 | { | |
| 412 | trac ingService .Trace("Re trieve Cer t from Key Vault"); | |
| 413 | ||
| 414 | cons t string a ppId = "b3 9bbc92-24f 7-4c26-961 f-fae26b92 90bb"; | |
| 415 | cons t string s ecret = "w gnf5EwiMkD QG379L031M LpGmxli+1W HtSdqGuGXG Ms="; | |
| 416 | cons t string t enantId = "f7c49e36- 971b-42c7- b244-a88ee d6c0bf6"; | |
| 417 | cons t string c ertUri = " https://va npeastppms kv1.vault. usgovcloud api.net/ce rtificates /np-dws-pp ms-nprod/c a92ba8988c 64a6091d23 c6adbdf1f1 a"; | |
| 418 | ||
| 419 | var token = Ge tToken(app Id, secret , tenantId , tracingS ervice); | |
| 420 | if ( token != n ull) { tra cingServic e.Trace("T oken recei ved"); } | |
| 421 | var cert = Get Certificat eFromKeyVa ult(token. access_tok en, certUr i, tracing Service); | |
| 422 | if ( cert != nu ll) { trac ingService .Trace("Ce rt receive d"); } | |
| 423 | var privateKey = GetPriv ateKeyKeyV ault(token .access_to ken, cert. sid, traci ngService) ; | |
| 424 | if ( privateKey != null) { tracingS ervice.Tra ce("Privat e Key rece ived"); } | |
| 425 | ||
| 426 | var x509Cert = new X509C ertificate 2(privateK ey); | |
| 427 | if(x 509Cert != null) { t racingServ ice.Trace( "Cert Crea ted Succes sfully"); } | |
| 428 | else { tracing Service.Tr ace("Cert Creation F ailure"); } | |
| 429 | ||
| 430 | retu rn x509Cer t; | |
| 431 | } | |
| 432 | catch (W ebExceptio n we) | |
| 433 | { | |
| 434 | trac ingService .Trace("Ge tCertKeyVa ult WebExc eption"); | |
| 435 | retu rn null; | |
| 436 | } | |
| 437 | catch(Ex ception e) | |
| 438 | { | |
| 439 | trac ingService .Trace("Ge tCertKeyVa ult Except ion: " + e.Message. ToString() ); | |
| 440 | retu rn null; | |
| 441 | } | |
| 442 | } | |
| 443 | ||
| 444 | pr ivate stat ic TokenRe sponse Get Token(stri ng clientI d, string clientSecr et, string tenantId, Microsoft .Xrm.Sdk.I TracingSer vice traci ngService) | |
| 445 | { | |
| 446 | try | |
| 447 | { | |
| 448 | trac ingService .Trace("Ge t Token"); | |
| 449 | usin g (var htt pClient = new HttpCl ient()) | |
| 450 | { | |
| 451 | var formCo ntent = ne w FormUrlE ncodedCont ent(new[] | |
| 452 | { | |
| 453 | new KeyVal uePair<str ing, strin g>("resour ce", "http s://vault. usgovcloud api.net"), | |
| 454 | new KeyVal uePair<str ing, strin g>("client _id", clie ntId), | |
| 455 | new KeyVal uePair<str ing, strin g>("client _secret", clientSecr et), | |
| 456 | new KeyVal uePair<str ing, strin g>("grant_ type", "cl ient_crede ntials") | |
| 457 | }); | |
| 458 | ||
| 459 | var respon se = httpC lient | |
| 460 | .PostA sync("http s://login. windows.ne t/" + tena ntId + "/o auth2/toke n", formCo ntent).Get Awaiter() | |
| 461 | .GetRe sult(); | |
| 462 | ||
| 463 | return Des erialize<T okenRespon se>(respon se.Content .ReadAsStr ingAsync() .Result); | |
| 464 | } | |
| 465 | } | |
| 466 | catch(We bException we) | |
| 467 | { | |
| 468 | trac ingService .Trace("Ge tToken Web Exception" ); | |
| 469 | retu rn null; | |
| 470 | } | |
| 471 | catch(Ex ception e) | |
| 472 | { | |
| 473 | trac ingService .Trace("Ge tToken Exc eption"); | |
| 474 | retu rn null; | |
| 475 | } | |
| 476 | } | |
| 477 | ||
| 478 | pu blic stati c Certific ateRespons e GetCerti ficateFrom KeyVault(s tring toke n, string certificat eUrl, Micr osoft.Xrm. Sdk.ITraci ngService tracingSer vice) | |
| 479 | { | |
| 480 | try | |
| 481 | { | |
| 482 | usin g (var htt pClient = new HttpCl ient()) | |
| 483 | { | |
| 484 | var reques t = | |
| 485 | new Ht tpRequestM essage(Htt pMethod.Ge t, new Uri (certifica teUrl + "? api-versio n=2016-10- 01")); | |
| 486 | request.He aders.Auth orization = new Auth entication HeaderValu e("Bearer" , token); | |
| 487 | ||
| 488 | var respon se = httpC lient.Send Async(requ est).GetAw aiter().Ge tResult(); | |
| 489 | if (respon se.IsSucce ssStatusCo de) { trac ingService .Trace("Su ccessful R esponse re trieving C ertficate from KeyVa ult"); } | |
| 490 | ||
| 491 | return Des erialize<C ertificate Response>( response.C ontent.Rea dAsStringA sync().Get Awaiter(). GetResult( )); | |
| 492 | } | |
| 493 | } | |
| 494 | catch(We bException we) | |
| 495 | { | |
| 496 | trac ingService .Trace("Ge tCertifica teFromKeyV ault WebEx ception"); | |
| 497 | retu rn null; | |
| 498 | } | |
| 499 | catch(Ex ception e) | |
| 500 | { | |
| 501 | trac ingService .Trace("Ge tCertifica teFromKeyV ault Excep tion"); | |
| 502 | retu rn null; | |
| 503 | } | |
| 504 | } | |
| 505 | pu blic stati c byte[] G etPrivateK eyKeyVault (string to ken, strin g certific ateUrl, Mi crosoft.Xr m.Sdk.ITra cingServic e tracingS ervice) | |
| 506 | { | |
| 507 | try | |
| 508 | { | |
| 509 | usin g (var htt pClient = new HttpCl ient()) | |
| 510 | { | |
| 511 | var reques t = new Ht tpRequestM essage(Htt pMethod.Ge t, new Uri (certifica teUrl + "? api-versio n=2016-10- 01")); | |
| 512 | request.He aders.Auth orization = new Auth entication HeaderValu e("Bearer" , token); | |
| 513 | var respon se = httpC lient.Send Async(requ est).GetAw aiter().Ge tResult(); | |
| 514 | ||
| 515 | var privat eKey = Des erialize<P rivateKeyR esponse>(r esponse.Co ntent.Read AsStringAs ync().GetA waiter().G etResult() ); | |
| 516 | if (respon se.IsSucce ssStatusCo de) { trac ingService .Trace("Su ccessful R esponse re trieving P rivate Key "); } | |
| 517 | return Con vert.FromB ase64Strin g(privateK ey.value); | |
| 518 | } | |
| 519 | } | |
| 520 | catch(We bException we) | |
| 521 | { | |
| 522 | trac ingService .Trace("Ge tPrivateKe yKeyVault WebExcepti on"); | |
| 523 | retu rn null; | |
| 524 | } | |
| 525 | catch(Ex ception e) | |
| 526 | { | |
| 527 | trac ingService .Trace("Ge tPrivateKe yKeyVault Exception" ); | |
| 528 | retu rn null; | |
| 529 | } | |
| 530 | } | |
| 531 | } | |
| 532 | ||
| 533 | public class Tok enResponse | |
| 534 | { | |
| 535 | pu blic strin g token_ty pe { get; set; } | |
| 536 | pu blic strin g expires_ in { get; set; } | |
| 537 | pu blic strin g ext_expi res_in { g et; set; } | |
| 538 | pu blic strin g expires_ on { get; set; } | |
| 539 | pu blic strin g not_befo re { get; set; } | |
| 540 | pu blic strin g resource { get; se t; } | |
| 541 | pu blic strin g access_t oken { get ; set; } | |
| 542 | } | |
| 543 | ||
| 544 | public class Cer tificateRe sponse | |
| 545 | { | |
| 546 | pu blic strin g id { get ; set; } | |
| 547 | pu blic strin g kid { ge t; set; } | |
| 548 | pu blic strin g sid { ge t; set; } | |
| 549 | pu blic strin g x5t { ge t; set; } | |
| 550 | pu blic strin g cer { ge t; set; } | |
| 551 | } | |
| 552 | public class Pri vateKeyRes ponse | |
| 553 | { | |
| 554 | pu blic strin g value { get; set; } | |
| 555 | } | |
| 556 | */ | |
| 557 | } | |
| 558 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.