C# 帐户会话在注销后返回

C# 帐户会话在注销后返回,c#,asp.net,C#,Asp.net,在ASP.NET中,我在每个页面上都有一个小div作为帐户。在每个页面的页面加载中,我检查名为username的会话是否为null。如果是的话,我把login放在div中,否则我就把他们的名字写在那里,然后按一个按钮退出。注销会删除会话并加载登录div,而不是当前div。这一切都很好,但如果我注销然后按“上一步”按钮,则会返回带有帐户名的div。我该怎么修? 我的问题的一个例子 aspx: aspx.cs: if(Session["username"] != null){ accountD

在ASP.NET中,我在每个页面上都有一个小div作为帐户。在每个页面的页面加载中,我检查名为username的会话是否为null。如果是的话,我把login放在div中,否则我就把他们的名字写在那里,然后按一个按钮退出。注销会删除会话并加载登录div,而不是当前div。这一切都很好,但如果我注销然后按“上一步”按钮,则会返回带有帐户名的div。我该怎么修? 我的问题的一个例子

aspx:


aspx.cs:

if(Session["username"] != null){
 accountDiv = "<div>" + Session["username"].ToString() + 
"<form method='post' onsubmit='return true'><input type='submit' name='logout'></form></div>"
}
else{
 accountDiv = "<div>" +
"<form method='post' onsubmit='return true'>" +
"<input type='text' name='username'>" +
"<input type='submit' name='login'>" +
"</form></div>";
}
if(Request.Form["login"]!=null){
Session["username"] = Request.Form["username"];
accountDiv = (code that builds the div with the name as before)
}
if(Request.Form["logout"]!=null){
Session.RemoveAll();
accountDiv = (code that builds the div with the login as before)
}
if(会话[“用户名”!=null){
accountDiv=”“+会话[“用户名”]。ToString()+
""
}
否则{
accountDiv=“”+
"" +
"" +
"" +
"";
}
if(Request.Form[“login”]!=null){
会话[“用户名”]=请求。表单[“用户名”];
accountDiv=(用前面的名称构建div的代码)
}
if(Request.Form[“logout”]!=null){
Session.RemoveAll();
accountDiv=(与前面一样使用登录名构建div的代码)
}

下面是代码,ASP NET有很多内置类和帮助程序,下面是使用Razor视图的示例

@if (Request.IsAuthenticated)
{

  <strong>@User.Identity.Name</strong>
  <!-- Log Off Link -->

}else{
 <strong>Please log in</strong>
}
@if(Request.IsAuthenticated)
{
@User.Identity.Name
}否则{
请登录
}

您可能不会使用这样的表单。您应该有一个登录页面,该页面的表单在提交时会转到一个操作,该操作会让用户登录,然后重定向到主页面,该主页面将显示您想要的页面,在角落中显示用户名,并带有注销链接

然后,注销链接应转到将用户注销并重定向到登录页面的操作。您应该有代码来检查未登录的用户(这将处理后台功能),然后他们将被相应地重定向

public ActionResult Logout()
{
    if (UserState.IsLoggedIn)
    {
        // Log the user out
    }
    return RedirectToAction("Login");
}

[HttpGet]
public ActionResult Login()
{
    if (UserState.IsLoggedIn)
    {
        return RedirectToAction("Index");
    }

    return View(new LoginViewModel(UserState));
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (UserState.IsLoggedIn)
    {
        return RedirectToAction("Index");
    }

    // Do the login and redirect accordingly
    if (loginWasSuccess)
    {
        return RedirectToAction("Action", "Controller");
    }

    // Otherwise, add errors to model state and reload login page with errors.
   ModelState.AddModelError("InvalidLogon", "Some error message");

   return View(new LoginViewModel(UserState));
}

不知道,因为没有代码。我们无法修改描述。描述通常是模糊的,很少准确地描述程序执行的实际情况。如果他们这样做了,就不需要编程语言了。在这种情况下,它们的问题是双重的,因为很可能您已经描述了应该发生的事情,而不是实际发生的事情,否则您可能不会有bug,或者您至少会对其原因有更多的了解。我添加了一个示例。我将使用ASP.NET和相关技术中提供的许多内置身份验证和授权方法之一。你自己滚动总是充满风险,并且容易出现bug/看不见的漏洞。也许这里的答案会帮助ADyson你能解释一下你的意思吗?Nick答案对你不起作用!你能解释一下吗?请求对象有一个布尔值来知道调用是否来自经过身份验证的用户。identity.name将返回登录的用户名。与使用会话变量“手动”开发的功能相同。
public ActionResult Logout()
{
    if (UserState.IsLoggedIn)
    {
        // Log the user out
    }
    return RedirectToAction("Login");
}

[HttpGet]
public ActionResult Login()
{
    if (UserState.IsLoggedIn)
    {
        return RedirectToAction("Index");
    }

    return View(new LoginViewModel(UserState));
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (UserState.IsLoggedIn)
    {
        return RedirectToAction("Index");
    }

    // Do the login and redirect accordingly
    if (loginWasSuccess)
    {
        return RedirectToAction("Action", "Controller");
    }

    // Otherwise, add errors to model state and reload login page with errors.
   ModelState.AddModelError("InvalidLogon", "Some error message");

   return View(new LoginViewModel(UserState));
}