我正在尝试运行此代码:
File.WriteAllText(FilePath + Description + "-" + ID + ".txt", FileContent);
但除非我冒充用户,否则不能。在我的 web.config 中,我已将模拟设置为 true,如果我在那里设置凭据,则该行代码将按预期工作。
<identity impersonate="true" userName="domain\username" password="password" />
这不起作用:
<identity impersonate="true" />
当我运行此代码时:
System.Security.Principal.WindowsIdentity.GetCurrent()
我可以看到它填充了正确的用户名,并且模拟设置为 true
。
那么,当我的用户可以模拟时,为什么这行代码没有运行?
File.WriteAllText(FilePath + Description + "-" + ID + ".txt", FileContent);
请帮忙!
更新
这是我用来登录事件目录的登录方法。
[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);
}
最佳答案
Focusing on the question: So, why is this line of code not running ...
以下是不进行模拟或使用或不使用用户名/密码进行模拟的含义:
Impersonation is disabled
应用程序池标识用于运行应用程序代码。
<identity impersonate="true"/>
IUSR用于运行应用程序代码。
<identity impersonate="true" userName="accountname" password="password"/>
指定用户的身份将用于运行应用程序代码。
现在知道含义了,当您指定没有用户名和密码的身份模拟时,这意味着您要求使用 IUSR
运行应用程序代码它没有足够的权限访问文件系统上的指定路径。
解决问题:
- 您可能需要补助金
IUSR
获得更多许可。 - 或者您可能想通过指定在配置或代码中具有足够权限的用户名和密码来进行模拟
- 或者您可能希望在具有足够权限的应用程序池身份下运行应用程序。
- 或者您可能希望使用集成 Windows 身份验证和配置进行委派,如 explained here .
要了解有关身份和假冒的更多信息,请查看以下资源:
关于c# - ASP.NET MVC 模拟无法与表单例份验证一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60117633/