简体   繁体   English

使用表单授权模拟用户

[英]Impersonate User with Forms Authorization

I am using Forms Authorization to login to my web application against the active directory, what I am trying to do is when the user logins, impersonate that user.我正在使用表单授权针对活动目录登录到我的 Web 应用程序,我想要做的是在用户登录时模拟该用户。 But I am running into a few problems, when I enable impersonate either via IIS or web.config I get a 500 error, here is that section of my web.config:但是我遇到了一些问题,当我通过 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>

If I set my credentials in the identity element it works without adjusting my IIS:如果我在身份元素中设置我的凭据,它无需调整我的 IIS 即可工作:

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

Here is my authorization in my IIS, this is what its currently set too:这是我在 IIS 中的授权,这也是它当前设置的:

在此处输入图片说明

If I disable Anonymous and enable impersonation, I get a 500 error.如果我禁用匿名并启用模拟,我会收到 500 错误。

What am I doing wrong and how do I get Forms Authentication to work with Impersonation.我做错了什么以及如何让表单身份验证与模拟一起使用。

Here is my login Controller:这是我的登录控制器:

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

UPDATE更新

I got passed the 500 error via <validation validateIntegratedModeConfiguration="false" /> , but the impersonate is still not working unless I set the credentials.我通过<validation validateIntegratedModeConfiguration="false" />通过了 500 错误,但除非我设置凭据,否则模拟仍然无法正常工作。 Is there away I can set the credentials of the person logging in?我可以设置登录人的凭据吗?

UPDATE更新

When I run this code, I can see that it is populated with the correct username and impersonate is set to true, what am I doing wrong?当我运行此代码时,我可以看到它填充了正确的用户名,并且 impersonate 设置为 true,我做错了什么?

System.Security.Principal.WindowsIdentity.GetCurrent()

Focusing on this part: What I am trying to do is when the user logins, impersonate that user.关注这一部分:我想要做的是当用户登录时,模拟该用户。

What you are looking for is called delegation.您正在寻找的是所谓的委托。

Delegation without using username and password of the user relies on Integrated Windows Authentication .不使用用户名和密码的委派依赖于集成的Windows 身份验证 You cannot achieve it using Forms Authentication unless use username and password of the user and do protocol transition .除非使用用户的用户名和密码并进行协议转换,否则您无法使用Forms Authentication实现它。

For learning purpose, This post shows an example of how you can do it in code by using the username and password which you receive from login page.出于学习目的, 这篇文章展示了如何使用从登录页面收到的用户名和密码在代码中执行此操作的示例。

I know this may be disappointing, but if you need delegation, you should rely on Windows Authentication and configure browser, IIS and ASP.NET application.我知道这可能令人失望,但如果您需要委托,您应该依赖 Windows 身份验证并配置浏览器、IIS 和 ASP.NET 应用程序。 To see a complete guide take a look at How to configure an ASP.NET application for a delegation scenario .要查看完整指南,请查看如何为委派方案配置 ASP.NET 应用程序

This is not a complete guide of the configurations, however shows you the most important configurations:这不是配置的完整指南,但向您展示了最重要的配置:

  • Setup browser : To setup browser, for IE, you need to check Enable Windows Integrated Authentication in Advanced tab of Internet Options .设置浏览器:要设置浏览器,对于 IE,您需要在Internet 选项的高级选项卡中选中启用 Windows 集成身份验证
  • Setup IIS : To setup IIS, you need to disable all authentications on IIS including Anonymous Authentication and just enable Windows Authentication .设置 IIS :要设置 IIS,您需要禁用 IIS 上的所有身份验证,包括匿名身份验证,只启用Windows 身份验证

  • Setup ASP.NET Application : In the web.config you need to set <authentication mode="Windows" /> and also set <identity impersonate="true" /> and also <allow users="*" /><deny users="?" />设置 ASP.NET 应用程序:在 web.config 中,您需要设置<authentication mode="Windows" />并设置<identity impersonate="true" /><allow users="*" /><deny users="?" /> <allow users="*" /><deny users="?" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM