Skip to content

Instantly share code, notes, and snippets.

@germ13
Created March 23, 2018 05:56
Show Gist options
  • Select an option

  • Save germ13/f1476a12e26da7b5cd33dec776939e2c to your computer and use it in GitHub Desktop.

Select an option

Save germ13/f1476a12e26da7b5cd33dec776939e2c to your computer and use it in GitHub Desktop.
api impersonation in c#
[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" };
}
}
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