这里我创建登录页面来检查validateUSer,但我得到了例外:这里我发布了我的完整代码,请检查一次...
在这里输入代码
命名空间Loginusingmvc.Controllers { 公共类LoginController:Controller { // // GET:/ Login /
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult LogIn(string returnUrl = "")
{
if (User.Identity.IsAuthenticated)
{
return LogOut();
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
// [HttpPost]
public bool ValidateUser(string UserID, string Password)
{
if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Password))
return false;
using (ntc_salesEntities context = new ntc_salesEntities())
{
var user = (from u in context.user_master
where String.Compare(u.UserID, UserID, StringComparison.OrdinalIgnoreCase) == 0
&& String.Compare(u.Password, Password, StringComparison.OrdinalIgnoreCase) == 0
select u).FirstOrDefault();
return user != null;
}
}
[HttpPost]
public ActionResult Login(user_master model, string returnUrl = "")
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserID,model.Password))
{
FormsAuthentication.RedirectFromLoginPage(model.UserID,false);
return RedirectToAction("Index", "Login");
}
else
{
ModelState.AddModelError("", "Login details are wrong.");
}
}
return View(model);
}
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Login");
}
}
Route:
routes.MapRoute(
name: "Default1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id =
UrlParameter.Optional }
);
HTML
@model Loginusingmvc.Models.user_master
@{
ViewBag.Title = "LogIn";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>LogIn</h2>
@Html.ValidationSummary(true, "Login Failed, check details")
<div>
@using (Html.BeginForm("Login", "Login", new { returnUrl = Request.QueryString["returnUrl"] }, FormMethod.Post))
{
<fieldset>
<legend>Login Form</legend>
<div class="editor-label">@Html.LabelFor(u=> u.UserID)</div>
<div class="editor-field">@Html.TextBoxFor(u=> u.UserID)
@Html.ValidationMessageFor(u=> u.UserID)
</div>
<div class="editor-label">@Html.LabelFor(u=> u.Password)</div>
<div class="editor-field">@Html.PasswordFor(u=> u.Password)
@Html.ValidationMessageFor(u=> u.Password)
</div>
<input type="submit" value="Log In" name="Login" />
</fieldset>
}
</div>
的web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Loginusingmvc-20140516104010;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Loginusingmvc-20140516104010.mdf" providerName="System.Data.SqlClient" />
<add name="LoginEntities" connectionString="metadata=res://*/Models.Login.csdl|res://*/Models.Login.ssdl|res://*/Models.Login.msl;provider=MySql.Data.MySqlClient;provider connection string="server=206.225.85.212;user id=ntc;password=NTC@123;database=ntc_sales"" providerName="System.Data.EntityClient" />
<add name="ntc_salesEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=;user id=ntc;password=NTC@123;database=ntc_sales"" providerName="System.Data.EntityClient" />
<add name="DAConnection" connectionString="Data Source=;user id=;database=" providerName="MySql.Data.MySQLClient" />
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider,mysql.web" connectionStringName="DAConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"
/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add connectionStringName="MySqlMembershipConnection"
applicationName="/"
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider, mysql.web"
autogenerateschema="true"/>
</providers>
</roleManager>
<profile>
<providers>
<clear/>
<add type="MySql.Web.Security.MySqlProfileProvider, mysql.web"
name="MySqlProfileProvider"
applicationName="/"
connectionStringName="MySqlMembershipConnection"
autogenerateschema="true"/>
<remove name="MySQLProfileProvider" />
</providers>
</profile>
答案 0 :(得分:0)
需要修改assemblydependency
enter code here <dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>