简体   繁体   English

在MVC Razor中注销后用户按“后退”按钮时,如何防止身份验证?

[英]How to Prevent Authentication when User Press Back button after signout in MVC Razor?

I have written the code for preventing user Authentication in my GET Method like below 我已经在GET方法中编写了用于防止用户身份验证的代码,如下所示

if (!string.IsNullOrEmpty(Session["UserName"].ToString()))
            {
                MyConnection mycon = new MyConnection();
                string str = "";
                int res = 0;
                if (Request.QueryString.ToString().Contains("ID1"))
                {
                    str = "Delete from PostTable where PostID=" + Request.QueryString["ID1"];
                    res = mycon.IODPost(str);
                }
                return View(AllPostList());
            }
            else
            {
                return RedirectToAction("Home", "Home");
            }

but when i press Back button in browser after signout, the page is postback to the previous page which can't be done.. so what should i do to prevent this? 但是,当我注销后按浏览器中的“后退”按钮时,该页面将回退到上一页,这是无法完成的..那么我该怎么做才能防止这种情况发生?

so there are several bad practices in your example. 因此,您的示例中存在几种不良做法。

  1. be aware of sql injection - use ADO.NET SqlCommand and SqlCommandParameter classes to create queries. 注意SQL注入-使用ADO.NET SqlCommand和SqlCommandParameter类创建查询。

  2. ASP MVC has attributes like [Authorized] , [AllowAnonymous] that can guid your app workflow for each request. ASP MVC具有[Authorized]和[AllowAnonymous]之类的属性,可以针对每个请求引导您的应用程序工作流。 You can add these at the controller lever, or method level. 您可以在控制杆或方法级别添加这些。

  3. Check for null againts "Session["UserName"].ToString()" 检查null再次“ Session [” UserName“]。ToString()”

Some links : 一些链接:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlcommand.parameters.aspx

http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.108).aspx#using_authorizeattribute http://msdn.microsoft.com/zh-CN/library/system.web.mvc.authorizeattribute(v=vs.108).aspx#using_authorizeattribute

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么退出时会转到页面并在c#,mvc中按浏览器后退按钮 - why it goes to a page when signout and press browser back button in c#, mvc 超时后,如何将用户重定向回退出页面? Asp.net MVC - How can I redirect a user back to signout page when timed out? Asp.net MVC 如何防止用户在启用了缓存的asp.net mvc中注销后单击返回按钮 - How to prevent user to click on the back button after logout in asp.net mvc with cache enabled 用户按下GoBack硬件按钮后如何防止返回? - How to prevent going back after the user pressed the GoBack hardware button? 使用Razor MVC + Mysql进行用户身份验证 - User Authentication with Razor MVC + Mysql 如何使用后退按钮成功登录后阻止用户返回登录页面 - How to prevent user from going back to the login-page after successful login using back button 注销后,如何在ASP.NET MVC3中单击浏览器的后退按钮时将用户重定向到登录页面 - After logout,how to redirect user to login page when he/she will click back button of browser in ASP.NET MVC3 如何在 ASP.NET Core MVC 3.1 应用程序中注销后防止返回按钮 - How to prevent back button after logout in ASP.NET Core MVC 3.1 app 如何自动防止退出? - How to prevent signout automatically? 通过按 ESC 按钮防止用户关闭对话框 - Prevent user dismiss dialog by press ESC button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM