简体   繁体   English

因此,用户已登录,如何实现重定向到主页? (ASP MVC 3)

[英]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. 如果我对它的理解不正确,请纠正我。

  • You are hosting both the websites on localhost. 您将两个网站都托管在localhost上。 Most likely different port numbers. 最有可能是不同的端口号。
  • You logged into the dummy website (the template website) first. 您首先登录了虚拟网站(模板网站)。 You got back an auth cookie. 您返回了一个身份验证Cookie。
  • You didn't log out of the dummy website. 您没有退出虚拟网站。 So, your auth cookie is still valid. 因此,您的身份验证Cookie仍然有效。
  • You then launched the actual website (your project). 然后,您启动了实际的网站(您的项目)。 When you request the signin page, the cookie which you received earlier is also sent to the server. 当您请求登录页面时,先前收到的cookie也将发送到服务器。 The reason this happens is the cookie is scoped to the domain localhost and to the app path \\ . 发生这种情况的原因是cookie的范围是域localhost和应用程序路径\\ This matches both the websites. 这与两个网站都匹配。
  • You see yourself as logged in and as someone else. 您会看到自己已登录,并且与其他人一样。

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.

相关问题 301我如何重定向到主页? - how do i 301 Redirect to home page? 用户已经登录后,如何重定向到未授权页面而不是登录页面? - How do I redirect to a not-authorized page instead of the login page when the user is already logged in? 如何在没有EF的ASP MVC中的页面中显示登录用户的名称 - How to show the name of the logged user in the Page in ASP MVC without EF 如何在ASP.NET MVC中维护有关用户是否在整个页面中“登录”的信息? - How in ASP.NET MVC do I maintain info about whether a user is “logged in” throughout pages? 当用户未登录 ASP.NET MVC 时重定向到页面的最简单方法是什么? - Which is the simplest way to redirect to page when user is not logged in in ASP.NET MVC? asp.net mvc6 授权重定向在用户未登录时不起作用 - asp.net mvc6 Authorization redirect do not work when user is not logged 如何将错误重定向到MVC主页? - How to redirect errors to home page in MVC? 超时后,如何将用户重定向回退出页面? Asp.net MVC - How can I redirect a user back to signout page when timed out? Asp.net MVC 用户登录并尝试返回登录屏幕后,如何将用户重定向到视图ASP NET CORE - How do I redirect the user to a view once they've logged in and attempted to go back to the login screen ASP NET CORE 如果只有登录用户,如何重定向或访问页面 - How to redirect or visit page if only there is a logged in user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM