Produced by Araxis Merge on 2/1/2017 2:56:32 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\GeneralProgramming\Authentication | ImpersonateWithOnBehalfOfPrivilege.cs | Tue Dec 20 19:51:46 2016 UTC |
| 2 | Wed Feb 1 19:56:32 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 241 |
| 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 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 3 | // | |||||
| 4 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 5 | // | |||||
| 6 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 7 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 8 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 9 | // | |||||
| 10 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 11 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 12 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 13 | // PARTIC ULAR PURPO SE. | |||||
| 14 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 15 | ||||||
| 16 | //<snippet Impersonat eWithOnBeh alfOfPrivi lege> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Service Model; | |||||
| 19 | using Syst em.Linq; | |||||
| 20 | ||||||
| 21 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 22 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 23 | using Micr osoft.Xrm. Sdk; | |||||
| 24 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | ||||||
| 27 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 28 | { | |||||
| 29 | /// <s ummary> | |||||
| 30 | /// De monstrates how to im personate another us er with th e on-behal f-of privi lege and | |||||
| 31 | /// do basic ent ity operat ions like create, re trieve, up date, and delete.</s ummary> | |||||
| 32 | /// <r emarks> | |||||
| 33 | /// Th e system u ser accoun t under wh ich you ru n the samp le must be part of t he | |||||
| 34 | /// Ad ministrato rs group o n your com puter syst em so that this samp le can cre ate | |||||
| 35 | /// an d use a se cond user account. Y ou must al so have th e | |||||
| 36 | /// “A ct on Beha lf of Anot her User” privilege in Microso ft Dynamic s CRM. | |||||
| 37 | // / | |||||
| 38 | // / Note tha t the effe ctive set of privile ges for th e operatio ns perform ed will be the | |||||
| 39 | // / intersec tion of th e privileg es that th e logged o n (privile ged) user possesses with | |||||
| 40 | // / that of the user t hat is bei ng imperso nated. | |||||
| 41 | /// | |||||
| 42 | /// At run-time, you will be given t he option to delete all the | |||||
| 43 | /// da tabase rec ords creat ed by this program.< /remarks> | |||||
| 44 | public class Imp ersonateWi thOnBehalf OfPrivileg e | |||||
| 45 | { | |||||
| 46 | #r egion Clas s Level Me mbers | |||||
| 47 | ||||||
| 48 | pr ivate Guid _userId; | |||||
| 49 | pr ivate Guid _accountI d; | |||||
| 50 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 51 | ||||||
| 52 | #e ndregion C lass Level Members | |||||
| 53 | ||||||
| 54 | #r egion How To Sample Code | |||||
| 55 | // / <summary > | |||||
| 56 | // / This met hod connec ts to the Organizati on service using an impersonat ed user | |||||
| 57 | // / credenti al. Afterw ards, basi c create, retrieve, update, an d delete e ntity | |||||
| 58 | // / operatio ns are per formed as the impers onated use r. | |||||
| 59 | // / </summar y> | |||||
| 60 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 61 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete | |||||
| 62 | // / all crea ted entiti es.</param > | |||||
| 63 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 64 | { | |||||
| 65 | try | |||||
| 66 | { | |||||
| 67 | //<s nippetImpe rsonateWit hOnBehalfO fPrivilege 1> | |||||
| 68 | //<s nippetImpe rsonateWit hOnBehalfO fPrivilege 2> | |||||
| 69 | // C onnect to the Organi zation ser vice. | |||||
| 70 | // T he using s tatement e nsures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 71 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri,server Config.Cre dentials, serverConf ig.DeviceC redentials )) | |||||
| 72 | { | |||||
| 73 | // This st atement is required to enable early-boun d type sup port. | |||||
| 74 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 75 | ||||||
| 76 | CreateRequ iredRecord s(); | |||||
| 77 | ||||||
| 78 | // Retriev e the syst em user ID of the us er to impe rsonate. | |||||
| 79 | Organizati onServiceC ontext org Context = new Organi zationServ iceContext (_serviceP roxy); | |||||
| 80 | _userId = (from user in orgCon text.Creat eQuery<Sys temUser>() | |||||
| 81 | where user .FullName == "Kevin Cook" | |||||
| 82 | select use r.SystemUs erId.Value ).FirstOrD efault(); | |||||
| 83 | ||||||
| 84 | // To impe rsonate an other user , set the Organizati onServiceP roxy.Calle rId | |||||
| 85 | // propert y to the I D of the o ther user. | |||||
| 86 | _servicePr oxy.Caller Id = _user Id; | |||||
| 87 | ||||||
| 88 | // Instant iate an ac count obje ct. | |||||
| 89 | // See the Entity Me tadata top ic in the SDK docume ntation to determine | |||||
| 90 | // which a ttributes must be se t for each entity. | |||||
| 91 | Account ac count = ne w Account { Name = " Fourth Cof fee" }; | |||||
| 92 | ||||||
| 93 | // Create an account record na med Fourth Coffee. | |||||
| 94 | _accountId = _servic eProxy.Cre ate(accoun t); | |||||
| 95 | Console.Wr ite("{0} { 1} created , ", accou nt.Logical Name, acco unt.Name); | |||||
| 96 | //</snippe tImpersona teWithOnBe halfOfPriv ilege2> | |||||
| 97 | ||||||
| 98 | // Retriev e the acco unt contai ning sever al of its attributes . | |||||
| 99 | // Created By should reference the impers onated Sys temUser. | |||||
| 100 | // Created OnBehalfBy should re ference th e running SystemUser . | |||||
| 101 | ColumnSet cols = new ColumnSet ( | |||||
| 102 | "name" , | |||||
| 103 | "creat edby", | |||||
| 104 | "creat edonbehalf by", | |||||
| 105 | "addre ss1_postal code", | |||||
| 106 | "lastu sedincampa ign"); | |||||
| 107 | ||||||
| 108 | Account re trievedAcc ount = | |||||
| 109 | (Accou nt)_servic eProxy.Ret rieve(Acco unt.Entity LogicalNam e, | |||||
| 110 | _a ccountId, cols); | |||||
| 111 | Console.Wr ite("retri eved, "); | |||||
| 112 | ||||||
| 113 | // Update the postal code attr ibute. | |||||
| 114 | retrievedA ccount.Add ress1_Post alCode = " 98052"; | |||||
| 115 | ||||||
| 116 | // The add ress 2 pos tal code w as set acc identally, so set it to null. | |||||
| 117 | retrievedA ccount.Add ress2_Post alCode = n ull; | |||||
| 118 | ||||||
| 119 | // Shows u se of a Mo ney value. | |||||
| 120 | retrievedA ccount.Rev enue = new Money(500 0000); | |||||
| 121 | ||||||
| 122 | // Shows u se of a bo olean valu e. | |||||
| 123 | retrievedA ccount.Cre ditOnHold = false; | |||||
| 124 | ||||||
| 125 | // Update the accoun t record. | |||||
| 126 | _servicePr oxy.Update (retrieved Account); | |||||
| 127 | Console.Wr ite("updat ed, "); | |||||
| 128 | ||||||
| 129 | // Delete the accoun t record. | |||||
| 130 | _servicePr oxy.Delete (Account.E ntityLogic alName, _a ccountId); | |||||
| 131 | Console.Wr iteLine("a nd deleted ."); | |||||
| 132 | ||||||
| 133 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 134 | } | |||||
| 135 | //</ snippetImp ersonateWi thOnBehalf OfPrivileg e1> | |||||
| 136 | } | |||||
| 137 | ||||||
| 138 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 139 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 140 | { | |||||
| 141 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 142 | thro w; | |||||
| 143 | } | |||||
| 144 | } | |||||
| 145 | ||||||
| 146 | // / <summary > | |||||
| 147 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 148 | // / </summar y> | |||||
| 149 | pu blic void CreateRequ iredRecord s() | |||||
| 150 | { | |||||
| 151 | // Creat e a second user that we will i mpersonate in our sa mple code. | |||||
| 152 | SystemUs erProvider .RetrieveS alesManage r(_service Proxy); | |||||
| 153 | } | |||||
| 154 | ||||||
| 155 | // / <summary > | |||||
| 156 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 157 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 158 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 159 | // / </summar y> | |||||
| 160 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 161 | { | |||||
| 162 | // For t his sample , all crea ted record s are dele ted in the Run() met hod. | |||||
| 163 | // The s ystem user named "Ke vin Cook" that was c reated by this sampl e will | |||||
| 164 | // conti nue to exi st on your system be cause syst em users c annot be d eleted | |||||
| 165 | // in Mi crosoft Dy namics CRM . They ca n only be enabled or disabled. | |||||
| 166 | } | |||||
| 167 | ||||||
| 168 | #e ndregion H ow To Samp le Code | |||||
| 169 | ||||||
| 170 | #r egion Main method | |||||
| 171 | ||||||
| 172 | // / <summary > | |||||
| 173 | // / Standard Main() me thod used by most SD K samples. | |||||
| 174 | // / </summar y> | |||||
| 175 | // / <param n ame="args" ></param> | |||||
| 176 | st atic publi c void Mai n(string[] args) | |||||
| 177 | { | |||||
| 178 | try | |||||
| 179 | { | |||||
| 180 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 181 | // c redentials from the user. | |||||
| 182 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 183 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 184 | ||||||
| 185 | Impe rsonateWit hOnBehalfO fPrivilege app = new Impersona teWithOnBe halfOfPriv ilege(); | |||||
| 186 | app. Run(config , true); | |||||
| 187 | } | |||||
| 188 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 189 | { | |||||
| 190 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 191 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 192 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 193 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 194 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 195 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 196 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 197 | } | |||||
| 198 | catch (S ystem.Time outExcepti on ex) | |||||
| 199 | { | |||||
| 200 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 201 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 202 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 203 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 204 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 205 | } | |||||
| 206 | catch (S ystem.Exce ption ex) | |||||
| 207 | { | |||||
| 208 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 209 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 210 | ||||||
| 211 | // D isplay the details o f the inne r exceptio n. | |||||
| 212 | if ( ex.InnerEx ception != null) | |||||
| 213 | { | |||||
| 214 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 215 | ||||||
| 216 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 217 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 218 | if (fe != null) | |||||
| 219 | { | |||||
| 220 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 221 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 222 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 223 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 224 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 225 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 226 | } | |||||
| 227 | } | |||||
| 228 | } | |||||
| 229 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 230 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 231 | ||||||
| 232 | finally | |||||
| 233 | { | |||||
| 234 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 235 | Cons ole.ReadLi ne(); | |||||
| 236 | } | |||||
| 237 | } | |||||
| 238 | #e ndregion M ain method | |||||
| 239 | } | |||||
| 240 | } | |||||
| 241 | //</snippe tImpersona teWithOnBe halfOfPriv ilege> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.