【发布时间】:2015-03-17 11:31:36
【问题描述】:
我是 Asp.Net MVC 的新手,如果它看起来很琐碎,很抱歉。
我正在关注本教程:http://techbrij.com/custom-roleprovider-authorization-asp-net-mvc
我的RedirectToAction 方法有问题,我在这样的登录操作后调用它:
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
using (AferSuiviDBEntities objContext = new AferSuiviDBEntities())
{
var objUser = objContext.People.FirstOrDefault(x => x.CompanyMail == model.Email && x.CompanyPassword == model.Password);
if (objUser == null)
{
ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
}
else
{
FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
Session["MyMenu"] = null;
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("RedirectToDefault");
}
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
我写了一个RedirectToDefault 方法来检查登录的用户并显示相应的页面:
public ActionResult RedirectToDefault()
{
String[] roles = Roles.GetRolesForUser();
if (roles.Contains("Administrator"))
{
return RedirectToAction("Index", "Home");
}
else if (roles.Contains("Engineer"))
{
return RedirectToAction("Index", "Engineer");
}
else if (roles.Contains("PublicUser"))
{
return RedirectToAction("Index", "PublicUser");
}
else
{
return RedirectToAction("Index");
}
}
我的 routeConfigfile 如下:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
}
问题是当我设置凭据然后点击“登录”按钮时,它来自这个 url:
http://localhost:3343/Account/Login
到这个:
http://localhost:3343/Account/Login?ReturnUrl=%2FAccount%2FRedirectToDefault
但登录页面仍然显示!谁能帮我解释一下为什么??
PS:我在RedirectToDefault方法中放了一个断点,看到里面没有进去!
更新
我的登录视图:
@model LoginViewModel
@{
ViewBag.Title = "Log in";
}
<h2>@ViewBag.Title.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
<p>
@Html.ActionLink("Register as a new user", "Register")
</p>
@* Enable this once you have account confirmation enabled for password reset functionality
<p>
@Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>*@
}
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm">
@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</section>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
更新 2
这是我的代码的图像,它表明该过程确实是我的 RedirectToAction 方法:
但是当点击 F11 时,它并没有进入我的“RedirectToDefault”方法,它完成了,但它只是刷新了我的第一个登录视图。
【问题讨论】:
-
Login视图中<form>标记的action属性是什么? -
你的方法
RedirectToDefault在哪里 - 在同一个控制器中?如果在FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe)行上设置断点,RedirectToDefault是否会被调用? -
@StephenMuecke 我为你更新帖子
-
不,我的意思是为
<form>标签生成的实际 html 是什么 -
@OllieP 是的!它一直持续到 return RedirectToAction("RedirectToDefault");指令,但是当我点击 F11 按钮时,它不是在方法 RedirectToDefault 中进入的!
标签: asp.net asp.net-mvc asp.net-mvc-4