Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

Need your help, the problem is as follows.

I want to restrict the direct url access when anybody tries to

access the pages by typing the direct url in the address bar of the browser

Thanks....
Posted
Comments
Harshil_Raval 4-Oct-13 1:27am    
Is this regarding login access or like it? Use session and check on page if session not available then redirect to your login page.
arvind bisht 4-Oct-13 1:43am    
but sir if someone already login, then session have value, i restrict the direct url access when anybody tries to
Harshil_Raval 4-Oct-13 2:30am    
Ok. i think you have some page it have one menu, and on click of menu it should redirect to that page, not by direct url. Right? Or clear your requirement.
arvind bisht 4-Oct-13 2:44am    
i want to restrict user to enter direct url.
Harshil_Raval 4-Oct-13 2:48am    
Your only way is to create global variable in global.asax. On click of any link first assign value to global variable. And in that link, on page load check for assigned value. if it match then valid. if not match then redirect to your invalid access page.

1 solution

Hi,
//Global.asax
C#
public class Global : System.Web.HttpApplication
{
        public static int value = 0;
       // all other functions....
}

Suppose you have your links in Menu.aspx page.
ASP.NET
<form id="form1" runat="server">
    <div>
        <asp:hyperlink id="hyp" runat="server" navigateurl="~/Check.aspx" text="Clickme" onclick="redirect(event,this)" xmlns:asp="#unknown"></asp:hyperlink>
        <asp:hyperlink id="hyp1" runat="server" navigateurl="~/Check1.aspx" text="Clickme1" onclick="redirect(event,this)" xmlns:asp="#unknown"></asp:hyperlink>
        <asp:hyperlink id="hyp2" runat="server" navigateurl="~/Check2.aspx" text="Clickme2" onclick="redirect(event,this)" xmlns:asp="#unknown"></asp:hyperlink>
    </div>
    <script type="text/javascript">
        function redirect(e,a) {
            e.preventDefault();
            var url = a.href;
            $.ajax({
                type: "POST",
                url: "Menu.aspx/SetValue",
                data: "{ 'URL': '" + url + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    window.location.assign(url);
                },
                failure: function (data) {
                }
            });
        }
    </script>
    </form>

// now your code behind
C#
[WebMethod]
        public static void SetValue(string URL)
        {
            string path = new Uri(URL).AbsolutePath;
            // set global value according to redirect url
            switch (path)
            { 
                case "/Check.aspx":
                    Global.value = 1;
                    break;
                case "/Check1.aspx":
                    Global.value = 2;
                    break;
                case "/Check2.aspx":
                    Global.value = 3;
                    break;
            }
        }

// one of your redirect to page in this example suppose Check.aspx
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               if (Global.value != 1)
               {
                   Response.Redirect("Menu.aspx");
               }
               else
               {
                   //if you strictly about this rule then make global value again 0.
                   //Global.value = 0;
               }
           }
       }

Hope it helps you.
Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900