这里我尝试使用asp.net MVC4应用程序创建登录页面,但无法使用HttpPost
[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(Models.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);
// }
}
登录:
@model Loginusingmvc.Models.user_master
@{
ViewBag.Title = "LogIn";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@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>
路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "LogIn", id =
UrlParameter.Optional }
);
}
答案 0 :(得分:1)
在不使用Memebership的情况下尝试这种方式:
[HttpPost]
public ActionResult Login(Models.user_master model, string returnUrl = "")
{
if (ValidateUser(model.UserID,model.Password))
{
FormsAuthentication.RedirectFromLoginPage(model.UserID,false);
return RedirectToAction("Index", "Login");
}
else
{
ModelState.AddModelError("", "Login details are wrong.");
}
return View(model);
}