Asp.net MVC ReturnUrl 值始终为空
Asp.net MVC ReturnUrl value always null
好的,在发布之前我已经研究并尝试了每一个建议(当然是单独的),但我每次都碰壁
这是我的登录视图 我使用 ViewBag 来传递 ReturnUrl 值,正如我在该问题的许多答案中看到的那样
<h2>Login</h2>
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
...............
这是登录操作的结果
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(UserLogin login, string returnUrl="")
{
string message = "";
using (NerdsContext nc = new NerdsContext())
{
var v = nc.User.Where(a => a.email == login.email).FirstOrDefault();
if (v != null)
{
if (!v.IsEmailVerified)
{
ViewBag.Message = "Please verify your email first";
return View();
}
if (string.Compare(Crypto.Hash(login.password), v.password) == 0)
{
int timeout = login.rememberMe ? 525600 : 20; // 525600 min = 1 year
var ticket = new FormsAuthenticationTicket(login.email, login.rememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
//Redirect the user to new url
if (Url.IsLocalUrl(returnUrl))
{
ViewBag.ReturnUrl = returnUrl;
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Nerd", "Home");
}
}
else
{
message = "Invalid credential provided";
}
}
else
{
message = "Invalid credential provided";
}
}
ViewBag.Message = message;
return View();
}
最后这是我在 web.config 文件
中添加的行
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="/Account/Login" timeout="30" slidingExpiration="true" protection="All"></forms>
</authentication>
当我 运行 我从来没有真正登录它总是让我回到登录页面并且 returnUrl 的值总是空的
那么这是怎么回事????
好的,经过大量搜索,我在这里找到了答案
[Request.IsAuthenticated is always false
]
我不得不在 web.config 的模块中添加这些行 system.WebServer
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
好的,在发布之前我已经研究并尝试了每一个建议(当然是单独的),但我每次都碰壁
这是我的登录视图 我使用 ViewBag 来传递 ReturnUrl 值,正如我在该问题的许多答案中看到的那样
<h2>Login</h2>
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
...............
这是登录操作的结果
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(UserLogin login, string returnUrl="")
{
string message = "";
using (NerdsContext nc = new NerdsContext())
{
var v = nc.User.Where(a => a.email == login.email).FirstOrDefault();
if (v != null)
{
if (!v.IsEmailVerified)
{
ViewBag.Message = "Please verify your email first";
return View();
}
if (string.Compare(Crypto.Hash(login.password), v.password) == 0)
{
int timeout = login.rememberMe ? 525600 : 20; // 525600 min = 1 year
var ticket = new FormsAuthenticationTicket(login.email, login.rememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
//Redirect the user to new url
if (Url.IsLocalUrl(returnUrl))
{
ViewBag.ReturnUrl = returnUrl;
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Nerd", "Home");
}
}
else
{
message = "Invalid credential provided";
}
}
else
{
message = "Invalid credential provided";
}
}
ViewBag.Message = message;
return View();
}
最后这是我在 web.config 文件
中添加的行 <authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="/Account/Login" timeout="30" slidingExpiration="true" protection="All"></forms>
</authentication>
当我 运行 我从来没有真正登录它总是让我回到登录页面并且 returnUrl 的值总是空的 那么这是怎么回事????
好的,经过大量搜索,我在这里找到了答案 [Request.IsAuthenticated is always false ]
我不得不在 web.config 的模块中添加这些行 system.WebServer
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />