I am trying to run this code:
File.WriteAllText(FilePath + Description + "-" + ID + ".txt", FileContent);
But cannot unless I am impersonating a user. In my web.config I have impersonate set to true, if I set the credentials there that line of code works as expected.
<identity impersonate="true" userName="domain\username" password="password" />
This does not work:
<identity impersonate="true" />
When I run this code:
System.Security.Principal.WindowsIdentity.GetCurrent()
I can see that it is populated with the correct username and impersonate is set to true
.
So, why is this line of code not running, when my user can impersonate?
File.WriteAllText(FilePath + Description + "-" + ID + ".txt", FileContent);
PLEASE HELP!
UPDATE
This is my login method that I am using to login against the active directory.
[HttpPost]
public ActionResult Index(Login model, string returnUrl)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}