c# - ASP.NET MVC 模拟无法与表单例份验证一起使用

标签 c# asp.net asp.net-mvc iis impersonation

我正在尝试运行此代码:

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/

相关文章:

c# - 编译器如何优化 C# 中的异常过滤器?

c# - c#中的另一个进程正在使用文件

c# - .NET Core 2.1 HttpClient 不返回预期值

带有自定义参数的 Html.DropDownListFor()

c# - 使用正则表达式(正则表达式)拆分数据

c# - InvalidOperationException 和 NullReferenceException

asp.net - Visual Studio ASP.NET 调试导致 SQL Server 在空闲时 CPU 使用率过高

asp.net - 如何从 ASPX 控件事件调用 Javascript 函数?

asp.net-mvc - 函数求值需要所有线程运行 - MVC

c# - 缩略图 shell 扩展 .NET