ASP.NET MVC 5,通过表单身份验证使用ADMembershipProvider显示我始终登录

时间:2015-03-05 19:40:23

标签: c# asp.net asp.net-mvc-4 asp.net-mvc-5 membership-provider

我可能在这里遗漏了一些东西,因为在设置Web.config和我的Controller后使用ADMembershipProvider,即使Global Filters [Authorize]我也是总是登录...我逐步完成了代码并注意到以下内容:

布局屏幕截图 enter image description here

我已连接到Corp域,但我的目标是使用Form Auth,以便用户需要使用其AD凭据登录而不是自动进行身份验证;似乎是这种情况。

[UPDATE] 我断开了我的WIFI,应用程序仍然向我显示我已通过身份验证,我也尝试在私人/隐身模式下运行,但没有运气。

[更新2] 我忘了提到我的前端的一个非常重要的方面,它的所有角度。我这样做的方式是,Controller使用View进行响应。视图加载角度后,通过$http.get$http.post下载所需内容。我注意到页面渲染正常但某些部分在随机时间给我错误。例如,我没有访问一个页面,它加载正常,但下拉框没有填充,当我仔细查看其中的数据是登录页面..所以它试图重定向我..怎么办我处理这个?如果是这样的话?

的Web.config

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <authentication mode="Forms">
      <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="45" slidingExpiration="false" protection="All" />
    </authentication>
    <membership defaultProvider="ADMembershipProvider">
      <providers>
        <clear />
        <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
      </providers>
    </membership>
  </system.web>

我的帐户控制器

 [AllowAnonymous]
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return this.View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }

            try
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                    return this.RedirectToAction("Index", "Home");
                }
            }
            catch (Exception ex)
            {
                //Fails if connection with LDAP can't be established.
                this.ModelState.AddModelError(string.Empty, "Internal Server Error, please check your Internet Connection. Unable to connect to Network!");
            }

            this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");

            return this.View(model);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return this.RedirectToAction("Login", "Account");
        }
    }

1 个答案:

答案 0 :(得分:1)

我的问题是windowsAuthentication默认启用IIS Express

在新的Windows 7 Pro 64位安装中测试后,我注意到当我调试应用程序时,它要求输入用户名和密码,由于某种原因,这不会出现在我的主机上(可能是存储了吗?)< / p>

enter image description here

所以我使用我的标准Windows登录凭据,并能够访问该页面。这解释了为什么@User.Identity.IsAuthorize总是显示为true,它使用我的本地计算机帐户来验证我。在研究了这个问题之后,我发现了如何删除它并启用anonymousAuthentication

<authentication>
    <anonymousAuthentication enabled="true" />
    <windowsAuthentication enabled="false" />
</authentication>

我找到了KB-837139,详细介绍了如何禁用此功能。

  

如何使用Internet服务管理器禁用集成Windows   IIS 7.0中的身份验证

     
      
  • 启动Internet服务管理器。
  •   
  • 展开包含网站,虚拟目录或要配置的文件的服务器   身份验证,然后展开站点。
  •   
  • 在控制台树中,单击要为其配置身份验证的网站或虚拟目录。
  •   
  • 在中心窗口框中,双击“身份验证”。
  •   
  • 在身份验证类型列表中,右键单击“Windows身份验证”,然后单击“禁用”
  •   
  • 退出Internet服务管理器。
  •   

在我的情况下,我必须通过转到位于applicationhost.config的{​​{1}}并打开文件来手动执行此操作。我发现在Visual Studio和C:\Users\UserAccount\Documents\IISExpress\config中加载它更容易。然后我找到了以下部分并手动进行了更改。

Toggle all Outlining