Tuesday 17 April 2012

Authenticate Pragmatically:Bind URL in TreeView Dynamically And Authorize User To View /Access Fix URLs

Authorize User To View/Access Specified URL From Tree View.
Bind URL in TreeView Dynamically For Particular User.
Authorize User To Access Fix URL for him..


Its Is Strictly saying to all reader that this is Not actual ASP.net authorization/authentication.
its just way of doing work in authenticate manner.
like proving different URL to different USER,It was just what i made for one of my project.and shared with you guys.




Let say you have a TreeView Control and its showing Its nodes and their sub nodes like middle one tree view sample from below image.

Now what you want is when you login,you'll see TreeView as it is in  left side.
At the same time when i Logged in from my end i'll see the same TreeView as it is in right side preview.





For my this post about TreeView,I'm Using Head Node and Child Node hope you understand what i mean to them..







Now As the TOP most Image we looked up a kind of image where i was saying..
that we provided...
particular CHILD NODE to particular HEAD NODE to particular USER...
So Lets first Define Head Node,then We Will Define Child Node and finally provide
access to the USER that which Nodes he can access..

So here we begin....
1)Define Head Node..








2)Define CHILD NODE...





3)Provide Access To Particular Child Nodes of Particular Head Nodes To User.












 4)And We Completed!!!
just for  a sample I am showing you a image which shows which kind of output you'll get in TreeView






 5)Now to remind you its not statically define....means User,Head Node,Child Node.....
Its all stored in database Dynamically.
For that we need FOUR important tables as shown below...
don't worry I've  attached it in download sample.





 6)And To Manage the Autherization/Create User  You must save details first as in below form..






7)As I know you better know how to add,edit update and delete SQL table from grid view.so lets move ahead..and come to the main point.
what we feel is every thing is bit easy other than how TreeView Understanding which thing to show or Not Show to the particular.

Its all because of our fill_Tree() which is binding on page load.and after getting proper data its Binding appropiate Nodes and Child Nodes for particular USER.




    void fill_Tree()
    {
        con.Open();
        SqlCommand SqlCmd = new SqlCommand("SELECT DISTINCT dbo.tbl_NodeParentTable.ParentName, dbo.tb_RoleManagement.Acc_Id, dbo.tbl_NodeParentTable.ParentID FROM         dbo.tb_RoleManagement INNER JOIN                       dbo.tbl_NodeAuthorizeTo ON dbo.tb_RoleManagement.Acc_Id = dbo.tbl_NodeAuthorizeTo.UserName INNER JOIN                       dbo.tbl_NodeParentTable ON dbo.tbl_NodeAuthorizeTo.Node = dbo.tbl_NodeParentTable.ParentID where Acc_Id=" + Request.Cookies["RoleID"].Value + "", con);
        SqlDataReader Sdr = SqlCmd.ExecuteReader();
        SqlCmd.Dispose();
        string[,] ParentNode = new string[100, 2];
        int count = 0;
        #region "Collect Parent Heading"
        while (Sdr.Read())
        {
            ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("ParentID")).ToString();
            ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("ParentName")).ToString();
        }
        #endregion
        Sdr.Close();
        //below loop will execute per Heading
        for (int loop = 0; loop < count; loop++)
        {
            TreeNode root = new TreeNode();
            root.Text = ParentNode[loop, 1];
            root.Target = "";
            root.NavigateUrl = "";
            SqlCommand Module_SqlCmd = new SqlCommand("SELECT     dbo.tb_RoleManagement.UserName, dbo.tb_RoleManagement.Acc_Id, dbo.tb_RoleManagement.Password, dbo.tbl_NodeAuthorizeTo.Node,                        dbo.tbl_NodeAuthorizeTo.ChildNode, dbo.tbl_NodeChildTable.ChildName, dbo.tbl_NodeChildTable.URL FROM         dbo.tb_RoleManagement INNER JOIN                       dbo.tbl_NodeAuthorizeTo ON dbo.tb_RoleManagement.Acc_Id = dbo.tbl_NodeAuthorizeTo.UserName INNER JOIN                       dbo.tbl_NodeChildTable ON dbo.tbl_NodeAuthorizeTo.ChildNode = dbo.tbl_NodeChildTable.Snno where Acc_Id=" + Request.Cookies["RoleID"].Value + "  and dbo.tbl_NodeAuthorizeTo.Node =" + ParentNode[loop, 0], con);
            SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();
            //below loop will execute Sub Nodes of Single heading Coming From above for loop
            while (Module_Sdr.Read())
            {
              
                TreeNode child = new TreeNode();
                child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("ChildName")).ToString();
                child.Target = "";
                child.NavigateUrl = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("URL")).ToString();
                root.ChildNodes.Add(child);

            }
            Module_Sdr.Close();

            // Add root node to TreeView
            TreeView1.Nodes.Add(root);

        }

        /*
        * By Default, when you populate TreeView Control programmatically, it expends all nodes.
        */
        TreeView1.CollapseAll();
        con.Close();

    }



8)And Finally We Called Our fill_Tree() Function.


    protected void Page_Load(object sender, EventArgs e)
    {        
        fill_Tree();
       
    }

9)In Between all these few times you may also required that you provided a fix time for user to access the page,other wise redirect him to timeout.html page!

So just look up at my page (AcessOnTime.cs page under Authentication Folder )you'll understand what i did with storing User Id in cookies.
So After you login just open AcessOnTime.aspx and you'll see the currect page (with msg time is on) if you logged in time.but if you logged in a time which is out of define time for you  you'll simply redirected to timeout.html page.

And finally here is what you're lookin for...

No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...