我正在使用 MVC3/Razor。登录网站后,出现主页,但我不明白为什么会出现上一页网址。
场景:当我将用户从登录页面重定向到主页时,地址栏中的 url 显示为“http://localhost:55104/Account/LogOn”而不是“http://localhost:55104/Home/Index”
帐户控制器
// GET: /Account/LogOn
public ActionResult LogOn()
{
if (HttpContext.User.Identity.IsAuthenticated == true)
{
return RedirectToAction("Index", "Home");
}
else
{
return View();
}
}
// POST: /Account/LogOn
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
try
{
if (AppAuthentication.Authenticate(model.UserName, model.Password, "10.0.3.18"))
{
string userName = model.UserName;
var user = new List<String>();
MySqlConnection con = DAL.GetMySqlConnection();
MySqlCommand cmd = new MySqlCommand("SELECT user_id, user_fname FROM users WHERE user_code='" + userName + "' AND user_treeCode <> 'xxx'", con);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
user.Add(rdr.GetString(0));
user.Add(rdr.GetString(1));
}
con.Close();
FormsAuthentication.SetAuthCookie(user[0], model.RememberMe);
Session["userID"] = user[0];
Session["userName"] = user[1];
return RedirectToAction("Index", "Home");
}
else
{
ViewBag.OpenID = "Invalid credentials ";
}
}
catch (Exception ex)
{
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Gloabal.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}