MVC 3我怎样才能让用户查看警告/免责声明屏幕 [英] MVC 3 How can I make a user view a warning/disclaimer screen

查看:260
本文介绍了MVC 3我怎样才能让用户查看警告/免责声明屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这将是很简单,但我挣扎了一点。我工作的一个项目使用MVC 3,要求用户使用本网站之前同意在一定条件下的客户端。我创建了一个标准的同意/不同意时,首先进入该网站里面装的屏幕,但如果用户键入该网站的不同部分的地址,他们可以绕过例如www.test.com加载条件的情况下,但如果用户类型www.test.com/home~~V他们绕过了条件。

I thought this would be very simple but I'm struggling a little. I'm working on a project for a client using MVC 3 that requires users to agree to certain conditions before using the site. I have created a standard agree/disagree screen which is loaded when first coming into the site, but if a user types a address to a different part of the site they can bypass the conditions for example www.test.com loads the conditions but if the user types www.test.com/home they bypass the conditions.

我怎样才能确保他们同意的条件,他们可以得到其他地方网站上的过吗?我一直在努力会话变量,我认为这是要走的路,但有没有办法来检查每一个页面请求这个变量,而不必写支票到网站上的每个控制器动作?

How can I make sure they have agreed to the conditions before they can get anywhere else on the site? I have been trying a session variable, which I think is the way to go, but is there a way to check this variable on every page request without having to write a check into every Controller Action on the site?

推荐答案

您可以做一个自定义属性,并将其添加到控制器的顶部。

You could make a custom attribute and add it to the top of the Controller.

例如:

    [AgreedToDisclaimer]
    public ActionResult LoadPage()
    {
         return View();
    }

如果AgreedToDisclaimer返回true,因为这只会加载视图。

Which would only load the view if the AgreedToDisclaimer returns true.

public class AgreedToDisclaimerAttribute : AuthorizeAttribute
{

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
       if (httpContext == null)
        throw new ArgumentNullException("httpContext");

       // logic to check if they have agreed to disclaimer (cookie, session, database)
       return true;
    }

   protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
   {
          // Returns HTTP 401 by default - see HttpUnauthorizedResult.cs.
           filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary 
            {
             { "action", "ActionName" },
             { "controller", "ControllerName" },
             { "parameterName", "parameterValue" }
            });
   }
}

<一个href=\"http://msdn.microsoft.com/en-us/library/dd410209(v=vs.90).aspx\">http://msdn.microsoft.com/en-us/library/dd410209(v=vs.90).aspx

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.handleunauthorizedrequest.aspx\">http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.handleunauthorizedrequest.aspx

这篇关于MVC 3我怎样才能让用户查看警告/免责声明屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆