简体   繁体   English

MVC 5非用户访问网站时如何显示消息?

[英]MVC 5 how to show message when a non-user visit the website?

I'm creating a program by using ASP.NET MVC 5. This program is needed for login and inside a page is order page. 我正在使用ASP.NET MVC 5创建程序。登录时需要该程序,并且页面内是订购页面。 How to make this page will show some string like "User is needed to login". 如何制作此页面将显示一些字符串,例如“需要用户登录”。

As my progress now, I only know to put the [Authorize] attribute 就我现在的进度而言,我只知道放入[Authorize]属性

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PlaceOrder(Order orderDetail)
        {
            String message = "";
            using (myDatabaseEntities1 myDatabase1 = new myDatabaseEntities1())
            {
                //WF
                Double PriceOfF1 = (orderDetail.A_ChickenChop_BP) * 14.9;
                Double PriceOfF2 = (orderDetail.A_ChickenChop_M) * 14.9;
                Double PriceOfF3 = (orderDetail.A_Spaghetti_AH) * 10.9;
                Double PriceOfF4 = (orderDetail.A_Spaghetti_P) * 10.9;
                Double PriceOfF5 = (orderDetail.A_Spaghetti_S) * 10.9;
                //CF
                Double PriceOfF6 = (orderDetail.A_ChickenRice_CB) * 6.9;
                Double PriceOfF7 = (orderDetail.A_ChickenRice_CW) * 6.9;
                Double PriceOfF8 = (orderDetail.A_ChickenRice_D) * 6.9;
                Double PriceOfF9 = (orderDetail.A_WantanMee_NS) * 6.9;
                Double PriceOfF10 = (orderDetail.A_WantanMee_IS) * 6.9;

                Double T_Price = orderDetail.OrderPrice;

                T_Price = PriceOfF1 + PriceOfF2 + PriceOfF3 + PriceOfF4 + PriceOfF5 +
                    PriceOfF6 + PriceOfF7 + PriceOfF8 + PriceOfF9 + PriceOfF10;

                if (T_Price > 1)
                {
                    myDatabase1.Orders.Add(orderDetail);
                    myDatabase1.SaveChanges();
                    message = "The order has been placed";
                    orderDetail.IsPlaced = true;
                }
                else
                {
                    message = "Please select at least one of the food";
                    orderDetail.IsPlaced = true;
                }
            }
            ViewBag.Message = message;
            return View();
        }
    }

When a non-user click this page, it will redirect to the userlogin page. 当非用户单击此页面时,它将重定向到userlogin页面。 How to make it a message will show when the non-user clicked 当非用户点击时,如何显示一条消息

This is one of the solutions. 这是解决方案之一。 on the page you want to show the message, add the following code on the exact location you want the message to appear 在您要显示消息的页面上,在您希望消息显示的确切位置上添加以下代码

  @if(!HttpContext.Current.User.Identity.IsAuthenticated)
       {
  <span class="your css class name">Please 
  <a href="@Url.Action("login action name")"> Login </a> to continue
    }

There is few ways to do it. 没有什么方法可以做到的。 You check inside this method if user is logged in (you have this information in request) and redirect him to page you want. 您可以在此方法内部检查用户是否已登录(在请求中有此信息),然后将其重定向到所需的页面。 It can be login page with return url to page he was before or maybe same page with information to log in - depends what you need. 它可以是登录页面,返回URL可以返回到他之前的页面,也可以是包含登录信息的页面-取决于您的需求。 Just type in google "mvc5 how to check if user is logged in". 只需输入google“ mvc5如何检查用户是否已登录”即可。 Other way is using attribute authorize or adding some custom filter. 另一种方法是使用属性授权或添加一些自定义过滤器。

With the Above code you can use [Authorize] filter. 通过上述代码,您可以使用[授权]过滤器。 In the Authorize filter you can validate the user is already logged in or logged in session exist or not. 在“授权”过滤器中,您可以验证用户是否已经登录或登录的会话是否存在。

If I understand your question correctly, all you need is to pop up a dialog or modal when a "un-authenticated" user clicks on "Place Order", Right ?. 如果我正确理解了您的问题,那么您需要做的就是在“未经身份验证”的用户单击“下订单”时弹出对话框或模式,对吗? If that is case , Don't redirect the user to your "PlaceOrder" post method, rather create a GET method which returns a JSON status(true or false) to check whether an authenticated user session is live or not ? 如果是这种情况,请不要将用户重定向到您的“ PlaceOrder”发布方法,而是创建一个返回JSON状态(真或假)的GET方法,以检查通过身份验证的用户会话是否有效? - Its that simple. -就这么简单。

Or if you need some more elegant, create a custom [Authorize] extending the "AuthorizationFilterAttribute" from where you can control the view returned or check with HTTP status returned 或者,如果需要更优雅的方法,请创建一个自定义[Authorize],扩展“ AuthorizationFilterAttribute”,从中可以控制返回的视图或检查返回的HTTP状态

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM