简体   繁体   English

ASP .Net Web.config

[英]ASP .Net Web.config

In my ASP .Net Application (Not MVC, Just ASP .Net) I have several web forms and need to restrict users from directly accessing to several web pages. 在我的ASP .Net应用程序(不是MVC,Just ASP .Net)中,我有几个Web表单,需要限制用户直接访问多个Web页面。

But in the application links from other pages and according to the functionality of application, should be able to redirect(Response.Redirect or Form submission in post or get way) to those pages but strictly not directly entering url in to browser and access them. 但是在其他页面的应用程序链接中,根据应用程序的功能,应该能够将这些页面重定向(Response.Redirect或表单提交或获取方式),但严格不要直接在浏览器中输入url并访问它们。

I have tried the following in those page load events (In which needs to restrict direct access) and working really fine. 我在那些页面加载事件中尝试了以下内容(其中需要限制直接访问)并且工作得很好。

if (Request.UrlReferrer == null)
{
     Response.Redirect("~/Index.aspx", true);
}

But the problem I've got is I am relatively new to ASP .Net and wonder if this is the best way to do this. 但我遇到的问题是我对ASP .Net相对较新,并想知道这是否是最好的方法。

1). 1)。 Mainly I searched but needs to know if I can get this done using the web.config file. 主要是我搜索但需要知道我是否可以使用web.config文件完成此操作。 If so would be grateful if someone can explain how the web.config file should be to get this done... 如果有人能解释web.config文件应该如何完成,那么将不胜感激...

2). 2)。 Also I want to redirect user if he enter wrong URL within the site hosted domain name... For that I did the following in the web.config but wonder if this is correct. 此外,如果他在网站托管域名中输入错误的URL,我想重定向用户...为此,我在web.config中执行了以下操作,但想知道这是否正确。 Thanks... 谢谢...

<customErrors mode="On" defaultRedirect="~/Index.aspx">
    <error statusCode="404" redirect="~/Index.aspx" />
</customErrors>

You can add this to your web.config file to restrict specific users: 您可以将其添加到web.config文件以限制特定用户:

<authorization>
<allow users="user1, user2"/>
<deny users="?"/>
</authorization>

<location path="AccessDenied.aspx">
 <system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

On your C#, you can use Response.Redirect 在C#上,您可以使用Response.Redirect

protected void Application_EndRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Response.Status.StartsWith("401"))
    {
        HttpContext.Current.Response.ClearContent();
        Response.Redirect("AccessDenied.aspx");
    }
}

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

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