我正在尝试设置一个Intranet MVC应用程序,该应用程序通过表单身份验证对我们公司的AD进行身份验证;我们希望用户必须登录。发布到Login操作时,我收到以下异常:“要调用此方法,”Membership.Provider“属性必须是”ExtendedMembershipProvider“的实例。”还有其他人有这个问题吗?
的Web.config:
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://example.domain.com/DC=example,DC=domain,DC=com"/>
</connectionStrings>
<appSettings>
<add key="enableSimpleMembership" value="false" />
</appSettings>
<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>
的AccountController:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
//The call to WebSecurity.Login is what throws
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
答案 0 :(得分:7)
在VS2010中创建了一个MVC3站点,并使用ActiveDirectoryMembershipProvider。然后将MVC4 AccountController更改为使用旧的System.Web.Security而不是WebMatrix.WebData.WebSecurity。
从:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
为:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
答案 1 :(得分:0)
我的登录工作正常。这是我的注销无法正常工作。在任何情况下,我都将WebSecurity.Logout()更改为FormsAuthentication.SignOut(),这样就可以了。