如何阻止用户在asp.net应用程序中查看受限页面
问题描述:
我是Asp.net中的新成员,对此我并不了解。我有一个查询。如何阻止用户在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进入网页浏览器时,那么这也得到开放,用户可以看到我的,我不想显示的所有信息。
我想要的东西 - 网址http://www.example.com/net/afk/xyz应该在内部工作,但无法通过网络浏览器URL启动。
我试图在IIS中输入一些拒绝规则作为net \ afk,但对于那些,我的网址http://www.example.com/net/afk/xyz停止响应内部。
请帮我一下,我可以用这个做什么。这种要求是可能的吗?
答
据我了解你的问题,如果你希望你的网页不应该由网址直接打开,那么你可以通过这种方式做到这一点
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;
}
答
所以基本上你需要的是防止用户访问该网址直。这可以通过使用可应用于控制器,操作方法等的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规定类。但是,您仍然可以在内部将用户导航到此操作方法。
你可以发布你的routeConfig.cs代码更多的帮助。关于第二个问题,你可以使用asp .net mvc授权过滤器,以避免这种情况 –
我没有任何routeConfig.cs,我只是web.config。 – Aashi
签入项目中的app_Start文件夹。 –