1
我在设置自定义表单身份验证票据后重定向到安全操作时遇到问题。这里是正在发生的事情:ASP.Net MVC 4自定义授权票据重定向问题
- 我浏览到网站/首页/指数
- 我自动重定向到网站/帐号/登录
- 我有一个有效的用户登录/密码
- 的RedirecToUrl( )函数试图我重定向到站点/主页/指数,但我会自动返回到网站/帐号/登录
- 请求被验证。如果我手动导航到网站/家庭/索引,我允许英寸
任何人都可以摆脱任何光线?
我的HomeController:
[Authorize]
public ActionResult Index()
{
return View();
}
我的AccountController:
[HttpGet]
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
bool bLogin = MyAuthentication.Login(model.UserName, model.Password);
if (bLogin)
{
Response.Cookies.Add(MyAuthentication.GetAuthenticationCookie(model.UserName.ToLower(), model.RememberMe));
RedirectToUrl(returnUrl);
}
else
ModelState.AddModelError("", "That is not a valid Username/Password combination");
}
return View(model);
}
private ActionResult RedirectToUrl(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
这是我如何创建自定义车票(只是增加用户数据):
public static HttpCookie GetAuthenticationCookie(string UserName, bool persistLogin)
{
var userData = null; // Code removed for brevity
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
UserName,
DateTime.Now,
DateTime.Now.AddMinutes(20),
persistLogin,
userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
}