Asp.net mvc 4 MVC4表单针对AD的身份验证
我正在尝试设置一个内部网MVC应用程序,该应用程序通过表单身份验证对我们公司的广告进行身份验证;我们确实希望用户必须登录。我在发布到登录操作时遇到以下异常:“若要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例。“其他人有此问题吗?” Web.config:Asp.net mvc 4 MVC4表单针对AD的身份验证,asp.net-mvc-4,forms-authentication,Asp.net Mvc 4,Forms Authentication,我正在尝试设置一个内部网MVC应用程序,该应用程序通过表单身份验证对我们公司的广告进行身份验证;我们确实希望用户必须登录。我在发布到登录操作时遇到以下异常:“若要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例。“其他人有此问题吗?” Web.config: <connectionStrings> <add name="ADConnectionString" connectionString="L
<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>
在VS2010中创建了一个MVC3站点,并使其与ActiveDirectoryMembershipProvider一起工作。然后将MVC4 AccountController更改为使用旧的System.Web.Security,而不是WebMatrix.WebData.WebSecurity 发件人: 致:
我的登录正在工作。是我的注销不起作用。无论如何,我将WebSecurity.Logout()改为FormsAuthentication.SignOut(),效果很好。您使用的是WebMatrix吗?我自己也没用过,但可能是因为你不能在ActiveDirectory中使用WebMatrix吗?如果不添加enableSimpleMembership键,您仍然会遇到问题吗?通过使用DotPeek查看WebSecurity类的代码,我可以看到这样一条评论,它与您的应用程序设置不太匹配:
//允许使用来禁用成员资格/角色提供程序的注册作为默认设置。
这是一个全新的项目,在VS2012中使用MVC4 w/Forms Auth模板。如果“enableSimpleMembership”=true,则仍会引发异常。将“enableSimpleMembership”更改为“EnableSimpleMembershipKey”也没有效果。
[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);
}
[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);
}