2012-12-04 21 views
2

我試圖建立一個Intranet MVC應用程序進行身份驗證對我們公司的AD使用表單認證的認證;我們確實希望用戶必須登錄。發佈到登陸行動時,我收到以下異常:「來稱呼這種方法,‘Membership.Provider’屬性必須是一個實例‘ExtendedMembershipProvider’。」還有其他人有這個問題嗎?MVC4形式對AD

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

是否使用WebMatrix中?我自己並沒有使用它,但是可能是因爲你不能在ActiveDirectory中使用WebMatrix?如果您不添加enableSimpleMembership密鑰,您是否仍然遇到問題?看看使用DotPeek的WebSecurity類的代碼,我可以看到這個註釋,它與你的應用程序設置不太匹配://允許使用來禁用註冊會員/角色提供的default.' – levelnis

+0

它是使用MVC4 W A全新的項目/形式VS2012驗證模板。使用「enableSimpleMembership」= true,異常仍然被拋出。 – miyamotogL

+0

更改「enableSimpleMembership」到「EnableSimpleMembershipKey」,也沒有任何影響。 – miyamotogL

回答

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

我登錄了工作。這是我註銷不起作用。無論如何,我將WebSecurity.Logout()更改爲FormsAuthentication.SignOut()並且工作正常。