我尝试从导航栏上的下拉列表登录但是当我点击signin
按钮时,表单被提交为get方法,虽然我定义了FormMethod.Post
但仍然使用get方法提交了所有值在QueryString
中可见
这是我的代码和图像
点击Signin
后
CODE:
@using(Html.BeginForm("Login","Auth",FormMethod.Post,null))
{
@Html.TextBoxFor(m=>m.Email,new{@style = "margin-bottom: 15px;",@placeholder = "Email address",@size = "30",@type = "email",@required = ""})
@Html.PasswordFor(m=>m.Password,new{@style = "margin-bottom: 15px;",@placeholder = "Password",@size = "30"})
@Html.CheckBoxFor(m => m.RememberMe, new { @style = "float: left; margin-right: 10px;" })
@Html.LabelFor(m=>m.RememberMe)
<input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size: 13px;" type="submit" value="Sign In" />
}
控制器代码
[HttpGet]
public ActionResult Login(string returl)
{
if (!User.Identity.IsAuthenticated)
{
ViewBag.returl = returl;
return View();
}
string[] arr1 = System.Web.Security.Roles.GetRolesForUser(User.Identity.Name);
foreach (var v in arr1)
{
if (v.Contains("Administrator"))
{
return RedirectToAction("Index", "Admin");
}
else
{
return RedirectToAction("Index", "Home");
}
}
return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult Login(Login log, string returl)
{
if (ModelState.IsValid && Membership.ValidateUser(log.Email, log.Password))
{
if (((MyMembershipProvider)Membership.Provider).IsActive(log.Email))
{
FormsAuthentication.SetAuthCookie(log.Email, log.RememberMe);
string[] arr = System.Web.Security.Roles.GetRolesForUser(log.Email);
if (returl != null)
{
foreach (var v in arr)
{
if (v.Contains("Administrator"))
{
return RedirectToAction("Index", "Admin");
}
else
{
return Redirect(returl);
}
}
}
else
{
foreach (var v in arr)
{
if (v.Contains("Administrator"))
{
return RedirectToAction("Index", "Admin");
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}
else
{
ModelState.AddModelError("", "Please confirm ur email to Login!!!!");
}
}
else
{
ModelState.AddModelError("", "Incorrect username or password!!!");
}
return View();
}
解: 错误是由于主页面包含一个表单标记,其中呈现部分视图,导致它重定向到主页(索引函数)