Created
March 23, 2018 05:56
-
-
Save germ13/f1476a12e26da7b5cd33dec776939e2c to your computer and use it in GitHub Desktop.
api impersonation in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Authorize] | |
| public class ValuesController : ApiController | |
| { | |
| // GET api/values | |
| public IEnumerable<string> Get() | |
| { | |
| WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal; | |
| WindowsIdentity wi = (WindowsIdentity)wp.Identity; | |
| WindowsImpersonationContext wc = wi.Impersonate(); | |
| // this code accesses file system under | |
| // the caller identity | |
| //wc.Undo(); | |
| return new string[] { "value1", "value2" }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public ActionResult Test() | |
| { | |
| ViewBag.Message = "Your Message"; | |
| var url = "http://localhost:52853/api/values"; | |
| using (var client = new WebClient { UseDefaultCredentials = true }) | |
| { | |
| var responseArray = client.DownloadString(url); | |
| } | |
| //var httpClient = new HttpClient(); | |
| //var response = httpClient.GetStringAsync(new Uri(url)); | |
| //var really = response.Result; | |
| //var releases = JArray.Parse(really); | |
| //httpClient.Dispose(); | |
| return View(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment