Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to modify some code behind logic that sends the users to different pages based on their website permissions.

I want to put the website links and the website permissions in a table, query the table, and - based on the users permissions, display the output associated with that permission all on one page instead of using the current Server.Transfer("xxxx.aspx", true) logic that is already on the page.

How would I do this?

Here is what I inherited:

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;

namespace SchoolDistrictOne
{
    public partial class MainOffice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var username = User.Identity.Name;
                 

                SqlConnection MyConnection = new SqlConnection("server=myservername\\sql2008;database=schools;Trusted_Connection=True;");

                SqlDataAdapter MyDataAdapter = new SqlDataAdapter("UserPermissions", MyConnection);

                MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 40));

                MyDataAdapter.SelectCommand.Parameters["@username"].Value = (username);

                MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@userpermission", SqlDbType.VarChar, 40));

                MyDataAdapter.SelectCommand.Parameters["@userpermission"].Direction = ParameterDirection.Output;

                DataSet DS = new DataSet();

                MyConnection.Open();

                MyDataAdapter.Fill(DS, "UsersPermissions");

                Session.Add("Permissions", DS);

                string userpermission = null;

                userpermission = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();

                string permission1 = "Principals";
                string permission2 = "FirstGrade";
                string permission3 = "SecondGrade";
                string permission4 = "SupportStaff";
                string permission5 = "Cafeteria";
                string permission6 = "BldgStaff";
               

               


                if (userpermission == permission1)
                {
                    //Server.Transfer("Principals.aspx", true);
                }
                else if (userpermission == permission2)
                {
                    Server.Transfer("FirstGrade.aspx", true);
                }
                else if (userpermission == permission3)
                {
                    Server.Transfer("SecondGrade.aspx", true);
                }
                else if (userpermission == permission4)
                {
                    Server.Transfer("SupportStaff.aspx", true);
                }
                else if (userpermission == permission5)
                {
                    Server.Transfer("Cafeteria.aspx", true);
                }
                else if (userpermission == permission6)
                {
                    Server.Transfer("BldgStaff.aspx", true);
                }
                else
                {
                    Server.Transfer("NoPermissions.aspx", true);
                }


                MyConnection.Close();
            }
        }


    }


}


I want to query the database, have ONE page instead of seven and have that one page display the information - links to that grade's or department's page - based on the permissions and links returned from the database.

Normally, I would execute a stored procedure, display the results based on an "if permissions = "Principals" then
HTML
output>result1result1 hreflink 
etc...

If the permission is FirstGrade, display the associated permissions on this same, one page.

Can I do this or do I have to keep maintaining 7 different main pages?

What I have tried:

Google search, searched codeproject blogs and archives and questions, dotnetperls
Posted
Comments
j snooze 29-Sep-16 17:49pm    
I don't know if I understand everything you are asking for but my condolences to you for all that hardcoding I see. That is not scalable at all as more permissions are added it would be a mess. My guess is you are creating a permissions table that has the permission type and associated web page as the fields, and will call a stored proc to get the user, the permissions and associated web page to go with that from the database? If so, I think you can use a panel and do a controls.Add to a panel. I'm a bit rusty with my webforms(mvc is much nicer once you get the hang of it). You should be able to loop through your datareader/table/set of data and create a new Link control then add it to that panel using the controls.add(NewLinkControl)...does that make sense?
Member 10379103 29-Sep-16 18:15pm    
Yes - you are correct in describing what the logic is currently doing and yes - what you suggested makes sense.

Thanks for assist!
Karthik_Mahalingam 29-Sep-16 23:46pm    
does the 7 pages has same content?
ArunRajendra 29-Sep-16 23:52pm    
You can have different section and show / hide based on different permission. Other option is to create user control. Based on permission instantiate the respective control. This way the code is clean and separated. http://stackoverflow.com/questions/12761000/is-there-a-way-to-render-partial-views-in-webforms

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