【发布时间】:2020-05-23 20:05:36
【问题描述】:
我正在尝试运行此代码:
File.WriteAllText(FilePath + Description + "-" + ID + ".txt", FileContent);
但除非我冒充用户,否则不能。在我的 web.config 中,我将 impersonate 设置为 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);
}
【问题讨论】:
-
我假设您没有在您的站点上使用任何形式的身份验证,并且 IIS 配置为匿名访问?我还假设您的网络服务器已加入您的域?您在哪个版本的 IIS 上运行?失败时会出现什么错误?
-
我看到你打开了another question with an identical +500 reputation bounty。该问题更详细,但似乎与完全相同的问题有关。这是故意的吗?还是我遗漏的问题有细微差别?
-
this SO thread 对你有用吗?
-
如果您使用表单身份验证,模拟 Windows 用户身份的唯一方法是拥有用户的用户名和密码。
-
使用 Active Directory 登录!= Windows 身份验证。您正在使用
Membership.ValidateUser,无论您使用什么提供商,它都是表单身份验证。 Active Directory 在此身份验证中,扮演 Membership Database 的角色,仅此而已。
标签: c# asp.net asp.net-mvc iis impersonation