MVC4控制器上的LDAP实现

时间:2015-09-08 19:42:28

标签: c# asp.net-mvc asp.net-mvc-4 ldap authorize

我是ASP.net MVC的新手,我有这个使用Windows身份验证的控制器(xxxxDMZ\username,密码):

[Authorize]
public class ExController : Controller
{ 
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);
        ViewBag.user = User.Identity.Name;
    }

    public ActionResult Index()
    {
        return View();
    }
}

现在我需要使用不同的域(xxxx\username,密码)来访问此控制器。

的web.config:

<system.web>
    <httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>       
    <authentication mode="Windows" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
        <add namespace="GridMvc" />
      </namespaces>
    </pages>
    <membership>
      <providers>
        <clear />
      </providers>
    </membership>
  </system.web>

当前的IIS 7.5配置: 已启用Windows身份验证(已启用提供程序:Negotiate,NTLM),已启用匿名身份验证

备注:

  • 网站托管在服务器上,可以通过RDC访问 xxxDMZ\username
  • xxxDMZ\usernamexxx\username完全不同。
  • 当用户登录Windows时,他使用此帐户xxx\username
  • 我希望用户在不使用凭据的情况下浏览网站,我只需要他们在尝试访问上述控制器时使用凭据(xxxx \用户名,密码)。
  • 我的问题不是所有(xxxx \用户名,密码)用户都有( xxxxDMZ \用户名,密码)帐户。

1 个答案:

答案 0 :(得分:0)

我认为您需要为您的身份验证配置ActiveDirectoryMembershipProvider。

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
      <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>
  <connectionStrings>
      <add name="ADConnectionString" connectionString="LDAP://primary.mydomain.local:389/DC=MyDomain,DC=Local" />
  </connectionStrings>
</configuration>

请参阅http://www.schiffhauer.com/mvc-5-and-active-directory-authentication/