2011-03-17 48 views
18

MVC3 kullanıyorum ve kullanıcı kimlik doğrulamasını web.config dosyasına koydum. Bu, sqlserver kimlik doğrulamasını atlamaktır. web.config'de aşağıdaWeb.config dosyasındaki form kimlik doğrulaması

kodu:

<authentication mode="Forms"> 
     <forms loginUrl="~/Account/LogOn" timeout="2880" > 
     <credentials passwordFormat="Clear"> 
      <user name="test123" password="test123" /> 
     </credentials> 
     </forms> 
</authentication> 

bahsettiğim kullanıcı kimliği ve şifre ile giriş çalıştı, ben

Giriş olarak sayfada hata alıyorum başarısız oldu. Lütfen hataları düzeltin ve tekrar deneyin.

* The user name or password provided is incorrect. 

Ben MembershipService.ValidateUser(model.UserName, model.Password) yöntemle de başarısız AccountController.cs dosyasına hata ayıklama

.

+0

ayıklarken, o model.UserName = "test123'ait" ve model.Password = "test123'ait" doğrulayabilir? Bunu doğrulayabilirseniz, sorunu yalıtmaya yardımcı olur. –

+0

MemberShipProvider'ı kaydettiniz mi? –

+0

Web.config üzerinden kullanıcı kimlik doğrulaması kullanıyorum (herhangi bir veritabanı gerektirmez) Bunun için memberhipprovider'ı nasıl yaparım ya da üyeliğe hala sahip olmam gerekiyor mu? –

cevap

30

Eğer MembershipProvider.ValidateUser yöntemi (Membership.Provider yoluyla) dahili olarak kullanılan öğreneceksiniz standart ASP.NET MVC 3 AccountController.cs ve AccountModels.cs dosyaları inceleyecek olursak. Web.config'te şifre saklamak istiyorsanız, bunun yerine FormsAuthentication.Authenticate yöntemini kullanmalısınız. Örneğin

:

public class AuthorizationController : Controller 
{ 
    public ActionResult LogOn() 
    { 
     return View("LogOn"); 
    } 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult LogOn(string userName, string password, 
     bool rememberMe, string returnUrl) 
    { 
     if (!ValidateLogOn(userName, password)) 
      return View("LogOn"); 

     FormsAuthentication.SetAuthCookie(userName, rememberMe); 

     if (!string.IsNullOrEmpty(returnUrl)) 
      return Redirect(returnUrl); 
     else 
      return RedirectToAction("Index", "News"); 

    } 

    private bool ValidateLogOn(string userName, string password) 
    { 
     if (string.IsNullOrEmpty(userName)) 
      ModelState.AddModelError("username", "User name required"); 

     if (string.IsNullOrEmpty(password)) 
      ModelState.AddModelError("password", "Password required"); 

     if (ModelState.IsValid && !FormsAuthentication. 
      Authenticate(userName, password)) 
      ModelState.AddModelError("_FORM", "Wrong user name or password"); 

     return ModelState.IsValid; 
    } 

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

     return RedirectToAction("LogOn"); 
    } 
} 
+0

teşekkürler çok yararlı bilgiler ve kod çalıştı. –

+0

Benim için çalıştım! Teşekkür ederim. –

İlgili konular