Home
%3CLINGO-SUB%20id%3D%22lingo-sub-347782%22%20slang%3D%22en-US%22%3EMaking%20an%20Asynchronous%20Call%20using%20the%20Impersonation%20Identity%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-347782%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EIf%20you%20try%20to%20make%20an%20asynchronous%20call%2C%20you%20will%20notice%20that%20the%20thread%20that%20executes%20the%20call%20doesn't%20run%20under%20the%20same%20account%20as%20the%20thread%20that%20called%20it%2C%20assuming%20you%20are%20using%20impersonation.%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EThere%20are%20a%20number%20of%20ways%20to%20change%20this%20if%20you%20would%20like%20to%20have%20it%20use%20the%20same%20identity.%3C%2FP%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.1%3B%20margin-bottom%3A%2010px%3B%20margin-top%3A%2010px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId-1310869232%22%20id%3D%22toc-hId-1341173280%22%3EMethod%201%3C%2FH4%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EThe%20Thread%20used%20by%20BeginInvoke%20doesn't%20copy%20the%20windowsIdentity%20from%20the%20calling%20thread.%20You%20have%20to%20impersonate%20the%20new%20thread%20manually%3A%20%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3EBefore%20calling%20BeginInvoke%20save%20the%20current%20identity%20into%20a%20variable%3A%3C%2FP%3E%0A%3CDIV%20style%3D%22background-color%3A%20%23f4f4f4%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%2012pt%3B%20max-height%3A%20200px%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20width%3A%2097.5%25%3B%20word-spacing%3A%200px%3B%20padding%3A%204px%3B%20margin%3A%2020px%200px%2010px%200px%3B%20border%3A%201px%20solid%20gray%3B%22%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f4f4f4%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20black%3B%20display%3A%20block%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20line-height%3A%2012pt%3B%20overflow%3A%20visible%3B%20overflow-wrap%3A%20break-word%3B%20width%3A%20100%25%3B%20word-break%3A%20break-all%3B%20padding%3A%200px%3B%20margin%3A%200em%3B%20border%3A%201px%20none%20%23cccccc%3B%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3EDim%3C%2FSPAN%3E%20identity%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Eas%3C%2FSPAN%3E%20WindowsIdentity%0Aidentity%20%3D%20System.Security.Principal.WindowsIdentity.GetCurrent()%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EInside%20the%20asynchronously%20called%20method%20use%20this%20variable%20and%20execute%3A%3C%2FP%3E%0A%3CDIV%20style%3D%22background-color%3A%20%23f4f4f4%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%2012pt%3B%20max-height%3A%20200px%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20width%3A%2097.5%25%3B%20word-spacing%3A%200px%3B%20padding%3A%204px%3B%20margin%3A%2020px%200px%2010px%200px%3B%20border%3A%201px%20solid%20gray%3B%22%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f4f4f4%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20black%3B%20display%3A%20block%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20line-height%3A%2012pt%3B%20overflow%3A%20visible%3B%20overflow-wrap%3A%20break-word%3B%20width%3A%20100%25%3B%20word-break%3A%20break-all%3B%20padding%3A%200px%3B%20margin%3A%200em%3B%20border%3A%201px%20none%20%23cccccc%3B%22%3Eidentity.Impersonate%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EFrom%20that%20point%20on%20the%20asynchronous%20call%20uses%20the%20same%20privileges%20than%20the%20calling%20thread%20and%20it%20should%20not%20be%20a%20problem%20to%20execute%20the%20callback.%3C%2FP%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.1%3B%20margin-bottom%3A%2010px%3B%20margin-top%3A%2010px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId--1241287729%22%20id%3D%22toc-hId--1210983681%22%3EMethod%202%3C%2FH4%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EIf%20the%20thread%20making%20the%20asynchronous%20call%20creates%20the%20thread%20being%20used%2C%20and%20you%20are%20using%20.NET%202.0%20or%20later%2C%20you%20can%20set%20the%20following%20in%20the%20config%20file.%26nbsp%3B%20For%20ASP.NET%2C%20use%20the%20aspnet.config%20file%3A%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22code%22%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.4285%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20pre%3B%20word-break%3A%20break-all%3B%20word-spacing%3A%200px%3B%20padding%3A%209.5px%3B%20margin%3A%200px%200px%2010px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3E%3CCONFIGURATION%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%20%3CRUNTIME%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%20%20%20%3CALWAYSFLOWIMPERSONATIONPOLICY%20enabled%3D%22true%22%3E%3C%2FALWAYSFLOWIMPERSONATIONPOLICY%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%20%20%20%3CLEGACYIMPERSONATIONPOLICY%20enabled%3D%22false%22%3E%3C%2FLEGACYIMPERSONATIONPOLICY%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%20%3C%2FRUNTIME%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%26lt%3B%2Fconfiguration%26gt%3B%3C%2FCONFIGURATION%3E%3C%2FPRE%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.1%3B%20margin-bottom%3A%2010px%3B%20margin-top%3A%2010px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId-501522606%22%20id%3D%22toc-hId-531826654%22%3EMethod%203%3C%2FH4%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EAnother%20way%20to%20handle%20this%20is%20if%20you%20have%20an%20account%20that%20you%20want%20the%20asynchronous%20call%20to%20run%20under%2C%20you%20can%20impersonate%20that%20account%2C%20run%20what%20you%20need%20to%2C%20and%20then%20undo%20the%20impersonation.%26nbsp%3B%20The%20following%20code%20will%20do%20that%3A%3C%2FP%3E%0A%3CDIV%20style%3D%22background-color%3A%20%23f4f4f4%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%2012pt%3B%20max-height%3A%20200px%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20width%3A%2097.5%25%3B%20word-spacing%3A%200px%3B%20padding%3A%204px%3B%20margin%3A%2020px%200px%2010px%200px%3B%20border%3A%201px%20solid%20gray%3B%22%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f4f4f4%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20black%3B%20display%3A%20block%3B%20font-family%3A%20consolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Ccourier%2Cmonospace%3B%20font-size%3A%208pt%3B%20line-height%3A%2012pt%3B%20overflow%3A%20visible%3B%20overflow-wrap%3A%20break-word%3B%20width%3A%20100%25%3B%20word-break%3A%20break-all%3B%20padding%3A%200px%3B%20margin%3A%200em%3B%20border%3A%201px%20none%20%23cccccc%3B%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Eusing%3C%2FSPAN%3E%20System.Security.Principal%3B%0A...%0AWindowsIdentity%20wi%20%3D%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Enew%3C%2FSPAN%3E%20WindowsIdentity(userName%40fullyqualifieddomainName)%3B%0AWindowsImpersonationContext%20ctx%20%3D%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Enull%3C%2FSPAN%3E%3B%0A%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Etry%3C%2FSPAN%3E%0A%7B%0A%20%20ctx%20%3D%20wi.Impersonate()%3B%0A%20%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23008000%3B%22%3E%2F%2F%20Thread%20is%20now%20impersonating%3C%2FSPAN%3E%0A%7D%0A%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Ecatch%3C%2FSPAN%3E%0A%7B%0A%20%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23008000%3B%22%3E%2F%2F%20Prevent%20exceptions%20propagating.%3C%2FSPAN%3E%0A%7D%0A%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%230000ff%3B%22%3Efinally%3C%2FSPAN%3E%0A%7B%0A%20%20%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23008000%3B%22%3E%2F%2F%20Ensure%20impersonation%20is%20reverted%3C%2FSPAN%3E%0A%20%20ctx.Undo()%3B%0A%7D%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.1%3B%20margin-bottom%3A%2010px%3B%20margin-top%3A%2010px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId--2050634355%22%20id%3D%22toc-hId--2020330307%22%3EAdditional%20Information%3C%2FH4%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%200px%2010px%200px%3B%22%3EFor%20more%20information%2C%20you%20can%20check%20out%20these%20links%3C%2FP%3E%0A%3CUL%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoe%20ui%26amp%3Bquot%3B%2Ctahoma%2Carial%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Csans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20margin-bottom%3A%2010px%3B%20margin-top%3A%200px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%0A%3CLI%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%3CA%20title%3D%22How%20To-%20Use%20Impersonation%20and%20Delegation%20in%20ASP.NET%202.0%22%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23337ab7%3B%20text-decoration%3A%20none%3B%22%20href%3D%22http%3A%2F%2Fmsdn2.microsoft.com%2Fen-us%2Flibrary%2Fms998351.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EHow%20To-%20Use%20Impersonation%20and%20Delegation%20in%20ASP.NET%202.0%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%3CA%20title%3D%22ASP.NET%202.0%20Security%20Questions%20and%20Answers%20-%20Impersonation%22%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23337ab7%3B%20text-decoration%3A%20none%3B%22%20href%3D%22http%3A%2F%2Fwww.securityguidanceshare.com%2Findex.php%3Ftitle%3DASP.NET_2.0_Security_Questions_and_Answers_-_Impersonation_%2F_Delegation%26amp%3Bprintable%3Dyes%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EASP.NET%202.0%20Security%20Questions%20and%20Answers%20-%20Impersonation%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EAuthor%3A%20ASP.NET%20Debugging%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E

If you try to make an asynchronous call, you will notice that the thread that executes the call doesn't run under the same account as the thread that called it, assuming you are using impersonation.

There are a number of ways to change this if you would like to have it use the same identity.

Method 1

The Thread used by BeginInvoke doesn't copy the windowsIdentity from the calling thread. You have to impersonate the new thread manually:
Before calling BeginInvoke save the current identity into a variable:

Dim identity as WindowsIdentity
identity = System.Security.Principal.WindowsIdentity.GetCurrent()

Inside the asynchronously called method use this variable and execute:

identity.Impersonate

From that point on the asynchronous call uses the same privileges than the calling thread and it should not be a problem to execute the callback.

Method 2

If the thread making the asynchronous call creates the thread being used, and you are using .NET 2.0 or later, you can set the following in the config file.  For ASP.NET, use the aspnet.config file:

 

<configuration>
<runtime>
<alwaysFlowImpersonationPolicy enabled="true"/>
<legacyImpersonationPolicy enabled="false"/>
</runtime>
</configuration>

Method 3

Another way to handle this is if you have an account that you want the asynchronous call to run under, you can impersonate that account, run what you need to, and then undo the impersonation.  The following code will do that:

using System.Security.Principal;
...
WindowsIdentity wi = new WindowsIdentity(userName@fullyqualifieddomainName);
WindowsImpersonationContext ctx = null;
try
{
  ctx = wi.Impersonate();
  // Thread is now impersonating
}
catch
{
  // Prevent exceptions propagating.
}
finally
{
  // Ensure impersonation is reverted
  ctx.Undo();
}

Additional Information

For more information, you can check out these links

Author: ASP.NET Debugging