【问题标题】:How to stop a user to see restricted page in asp.net application如何阻止用户在 asp.net 应用程序中查看受限页面
【发布时间】:2016-09-22 21:58:37
【问题描述】:

我是 Asp.net 的新手,对它了解不多。我有一个查询。

我的 dotnet 应用程序 URL 是 http://www.example.com

对于我网站的内部行为,我在http://www.example.com/net/afk/xyz 上发送请求,网页上会显示响应。并且此路径不会显示在 Web 浏览器 URL 上。它只显示http://www.example.com/page

但我的问题是,如果有人在我的应用程序登录后直接在网络浏览器中输入http://www.example.com/net/afk/xyz,那么它也会打开,用户可以看到我不想显示的所有信息。

我想要一些东西 - url http://www.example.com/net/afk/xyz 应该在内部工作,但无法通过网络浏览器 URL 启动。

我尝试在 IIS 中输入一些拒绝规则为 'net\afk,但对于这些,我的 url http://www.example.com/net/afk/xyz 停止在内部响应。

请帮我解决这个问题。这种要求可以吗?

【问题讨论】:

  • 能否发布您的 routeConfig.cs 代码以获得更多帮助。关于第二个问题,您可以使用 asp .net mvc 授权过滤器来避免此类情况
  • 我没有任何 routeConfig.cs,我只有 web.config。
  • 签入项目中的 app_Start 文件夹。
  • 您是否在管理面板中使用角色?

标签: asp.net asp.net-mvc-4


【解决方案1】:

据我了解你的问题,如果你希望你的页面不应该直接通过 Url 打开,那么你可以这样做

Uri u = HttpContext.Current.Request.UrlReferrer;
        if (u != null)
            return true;
        else
        {
            //Here user try to hit your Page url directly
            //Then you can redirect that user to your default Page or Home Page
            return false;
        }

【讨论】:

    【解决方案2】:

    所以基本上你需要的是防止用户直接访问 url。这可以通过使用 mvc 的属性来实现,这些属性可以应用于控制器、动作方法等。因此,为此您将创建一个 NoDirectAccessAttribute 来阻止用户导航到操作方法。

    using System;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace TestProject
    {
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
        public class NoDirectAccessAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null ||
                            filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
                {
                    filterContext.Result = new RedirectToRouteResult(new
                                   RouteValueDictionary(new { controller = "Home", action = "Index", area = "" }));
                }
            }
        }
    }
    

    这可以按如下方式应用于您的操作方法:

    [NoDirectAccess]
    public ActionResult xyz()
    {
        return View();
    }
    

    如果用户尝试访问此操作方法,他将被重新路由到主页,如 NoDirectAccessAttribute 类中指定的那样。但是,您仍然可以在内部将用户导航到此操作方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 2014-09-28
      • 2014-11-22
      相关资源
      最近更新 更多