【问题标题】:Impersonate User with Forms Authorization使用表单授权模拟用户
【发布时间】:2020-05-23 17:42:23
【问题描述】:

我正在使用表单授权针对活动目录登录我的 Web 应用程序,我想做的是在用户登录时模拟该用户。但是我遇到了一些问题,当我通过 IIS 或 web.config 启用模拟时,我收到 500 错误,这是我的 web.config 的那部分:

<customErrors mode="Off"/>
<authentication mode="Forms">
  <forms name=".ADAuthCookie" loginUrl="~/Login/Index" timeout="45" slidingExpiration="false" protection="All" path="/" />
</authentication>
<identity impersonate="true" />
<membership defaultProvider="ADMembershipProvider">
  <providers>
    <clear />
    <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
  </providers>
</membership>

如果我在身份元素中设置我的凭据,它可以在不调整我的 IIS 的情况下工作:

<identity impersonate="true" userName="domain\username" password="password" />

这是我在 IIS 中的授权,这也是它当前设置的:

如果我禁用匿名并启用模拟,我会收到 500 错误。

我做错了什么以及如何让表单身份验证与模拟一起使用。

这是我的登录控制器:

[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);
}

更新

我通过&lt;validation validateIntegratedModeConfiguration="false" /&gt; 传递了 500 错误,但除非我设置凭据,否则模拟仍然无法工作。我可以设置登录人的凭据吗?

更新

当我运行此代码时,我可以看到它填充了正确的用户名,并且 impersonate 设置为 true,我做错了什么?

System.Security.Principal.WindowsIdentity.GetCurrent()

【问题讨论】:

  • HTTP 500 错误的内容是什么?这听起来很重要且相关。
  • 500 - 内部服务器错误。您要查找的资源有问题,无法显示。
  • 如果我转到 Web 应用程序中的任何页面,就会发生这种情况
  • 您需要启用详细的 HTTP 500 错误消息。您是否配置了错误日志记录?
  • 这是answered 多年前。

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


【解决方案1】:

专注于这部分:我要做的是当用户登录时,模拟该用户。

您要查找的内容称为委托。

不使用用户名和密码的委托依赖于集成Windows身份验证。除非使用用户的用户名和密码并执行protocol transition,否则您无法使用 Forms Authentication 实现它。

出于学习目的,This post 展示了如何使用从登录页面收到的用户名和密码在代码中执行此操作的示例。

我知道这可能令人失望,但如果您需要委托,您应该依赖 Windows 身份验证并配置浏览器、IIS 和 ASP.NET 应用程序。要查看完整指南,请查看How to configure an ASP.NET application for a delegation scenario

这不是配置的完整指南,而是向您展示最重要的配置:

  • 设置浏览器 : 要设置浏览器,对于 IE,您需要在 Internet 选项高级 选项卡中勾选启用 Windows 集成身份验证 em>。
  • 设置 IIS:要设置 IIS,您需要禁用 IIS 上的所有身份验证,包括 匿名身份验证,然后启用 Windows 身份验证 .

  • 设置 ASP.NET 应用程序:在 web.config 中,您需要设置 &lt;authentication mode="Windows" /&gt; 并设置 &lt;identity impersonate="true" /&gt;&lt;allow users="*" /&gt;&lt;deny users="?" /&gt;

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 2014-12-18
    • 2019-11-06
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多