[英]So the user has logged in, how do I implement a redirect to home page? (ASP MVC 3)
I am quite confused as how to implement this functionality. 我对如何实现此功能感到很困惑。 Initially, when the user visits the website, they see the sign in page. 最初,当用户访问网站时,他们会看到登录页面。
[HttpGet]
public ActionResult SignIn()
{
return View() ;
}
When the user enters the details, it calls: 用户输入详细信息时,它将调用:
[HttpPost]
public ActionResult Sign(SignInModel signInModel)
{
if(service.ValidateUser(signInModel.userName,signInModel.passWord))
{
FormsAuthentication.SetAuthCookie(signInModel.userName, true);return
return RedirectToAction("Index", "Home");
}
}
However, as a test I decided to go back to the log in page, so localhost/Account/SignIn , but it doesn't redirect me back to the home page.. so I tried some suggested answers to similar questions from SO: 但是,作为测试,我决定返回登录页面,即localhost / Account / SignIn ,但它没有将我重定向回首页。
[HttpGet]
public ActionResult SignIn()
{
if(HttpContext.User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Home");
}
return View() ;
}
But I didn't understand how it worked so I decided to debug it. 但是我不知道它是如何工作的,所以我决定调试它。 But It turns out it was using the wrong Identity
. 但是事实证明它使用了错误的Identity
。
To explain, I was using a default MVC template to get my project working, I logged in with James on that template. 为了说明,我使用默认的MVC模板来使项目正常工作,我在James上登录了该模板。 However with my own project, I logged in with Peter. 但是,在我自己的项目中,我以Peter登录。
But HttpContext.User.Identity
in my own project refers to the default website's James instead of Peter.. so there is obviously something wrong there, but what? 但是我自己的项目中的HttpContext.User.Identity
指的是默认网站的James, 而不是 Peter。。所以那里显然有问题,但是什么呢?
TL;DR how do I persist information like StackOverflow? TL; DR如何保存诸如StackOverflow之类的信息? The user should only see the sign in page when the session expires or the user signs out. 用户仅应在会话期满或退出时看到登录页面。
Correct me if I am haven't understood it properly. 如果我对它的理解不正确,请纠正我。
localhost
and to the app path \\
. 发生这种情况的原因是cookie的范围是域localhost
和应用程序路径\\
。 This matches both the websites. 这与两个网站都匹配。 What can you do to fix this? 您可以采取什么措施解决此问题?
Log out of both websites. 注销两个网站。 Don't login to the dummy website. 不要登录到虚拟网站。 Login to the actual website. 登录到实际网站。 Have fun. 玩得开心。
A more permanent fix? 更永久的解决方案?
Depends. 要看。 The problem happens because the cookie is too loosely scoped. 发生此问题的原因是cookie的范围过于宽松。 If its necessary to run both websites on localhost
, then you could configure your IDE to launch them under a folder. 如果必须在localhost
上运行两个网站,则可以将IDE配置为在文件夹下启动它们。 Meaning dummy website is launched at http://localhost:1234/dummy/
and actual is launched at http://localhost:4321/actual
. 意味着虚拟网站在http://localhost:1234/dummy/
上启动,而实际网站在http://localhost:4321/actual
。 For normal cookies you would directly set the path. 对于普通的Cookie,您可以直接设置路径。 For the auth cookie, you could use the FormsAuthentication.GetAuthCookie
method. 对于身份验证cookie,可以使用FormsAuthentication.GetAuthCookie
方法。
src: http://telligent.com/support/telligent_evolution_platform/w/documentation/common-things-to-check-when-using-forms-authentication.aspx src: http : //telligent.com/support/telligent_evolution_platform/w/documentation/common-things-to-check-when-using-forms-authentication.aspx
HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, true);
cookie.Path = "/dummy";
Response.Cookies.Add(cookie);
Edit: 编辑:
Seem's like the general consensus is to not set the path. 似乎普遍的共识是不要设定道路。 https://stackoverflow.com/a/6940281/30007 https://stackoverflow.com/a/6940281/30007
Cookie paths are case-sensitive, so: Cookie路径区分大小写,因此:
http://site/path http://site/PATH
Are 2 different cookies for the browser - none of them (IE, FX, Safari, Opera or Chrome) will send /PATH's cookie to /path or vice versa. 浏览器有2种不同的cookie-它们(IE,FX,Safari,Opera或Chrome)都不会将/ PATH的cookie发送到/ path,反之亦然。
I think what you looking for is answered over there: How do I redirect /Home to root? 我认为您正在寻找的答案在那里: 如何将/ Home重定向到root?
Edit: 编辑:
In this case try this: when you generate the LogOn link you may want to pass the returnUrl
parameter: 在这种情况下,请尝试以下操作:生成LogOn链接时,您可能需要传递returnUrl
参数:
@Html.ActionLink("Log On", "LogOn", "Account", new { returnUrl = Request.Url }, null)
In your specific case you can pass returnUrl
to be home page. 在您的特定情况下,您可以将returnUrl
传递为主页。 Sorry for confusion. 抱歉造成混乱。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.