protected void Page_Load(object sender, EventArgs e) { RSClient rs = null; Role[] roles = null; try { if (!IsPostBack) { // Create an instance of RSClient rs = new RSClient(); // Sets Url and credentials for the endpoint. rs.Url = String.Format("https://{0}:443/ReportServer/ReportService2010.asmx", ConfigurationManager.AppSettings["RSSERVER_NAME"]); NetworkCredential cred = new NetworkCredential( ConfigurationManager.AppSettings["RSUSERNAME"], ConfigurationManager.AppSettings["RSPASSWORD"], ConfigurationManager.AppSettings["RSSERVER_NAME"]); rs.Credentials = cred; rs.LogonUser(cred.UserName, cred.Password, cred.Domain); // Checks if the user is authenticated if (rs.CheckAuthenticated()) { // Displaying Logged in user on the web page lblLoggedInUser.Text = "Logged In User: "******"RSUSERNAME"].ToString(); // Retrieves all the reports items recursively. gvListChildren.DataSource = rs.ListChildren("/", true); gvListChildren.DataBind(); // Roles stored in a session object roles = new Role[1]; roles = rs.ListRoles("Catalog", null); Session["roles"] = roles; // The function adds columns to the GridView, and creates a DataTable AddColumns(); AddTable(); } } } catch { if (rs != null) { rs.Dispose(); } throw; } if (rs != null) { rs.Dispose(); } }
// Saves the users-permissions private void SavePolicies() { try { //Retrieve the table from the session object. DataTable dt = (DataTable)Session["ItemPermissions"]; int i = 0; int j; int rolesindex = 0; int usersindex = 0; bool dbroundtrip = false; // Identify the users with atleast one role selected. foreach (GridViewRow gvr in gvItemPermissions.Rows) { rolesindex = 0; usersindex++; for (int cells = 2; cells < gvr.Cells.Count; cells++) { if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { rolesindex++; } } if (rolesindex == 0) { usersindex--; } } // Based on number of users, create an array of policies. Policy[] newPolicy = new Policy[gvItemPermissions.Rows.Count]; if (usersindex < gvItemPermissions.Rows.Count) { dbroundtrip = true; } // Update users, and the corresponding permissions. foreach (GridViewRow gvr in gvItemPermissions.Rows) { Policy currentPolicy = new Policy(); currentPolicy.GroupUserName = gvr.Cells[1].Text.ToString(); rolesindex = 0; for (int cells = 2; cells < gvr.Cells.Count; cells++) { Role currentRole = new Role(); if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { rolesindex++; } } if (rolesindex != 0) { Role[] roles = new Role[rolesindex]; j = 0; for (int cells = 2; cells < gvr.Cells.Count; cells++) { Role currentRole = new Role(); if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { currentRole.Name = ((System.Web.UI.WebControls.DataControlFieldCell)(gvr.Cells[cells])).ContainingField.ToString(); roles[j] = currentRole; j++; } } currentPolicy.Roles = roles; newPolicy[i] = currentPolicy; i++; } // Let the user be associated with at least a single role. Do not allow deleting users. else { lblInfo.Text = string.Format("At least a single role must be selected for the user '{0}'.", currentPolicy.GroupUserName); ModalPopupExtender1.Show(); return; } } // Below code snippet reloads the users, and associated roles by making a round trip to the report server catalog db. RSClient rs = null; try { rs = new RSClient(); // Sets Url and credentials for the endpoint. rs.Url = String.Format("https://{0}:443/ReportServer/ReportService2010.asmx", ConfigurationManager.AppSettings["RSSERVER_NAME"]); NetworkCredential cred = new NetworkCredential( ConfigurationManager.AppSettings["RSUSERNAME"], ConfigurationManager.AppSettings["RSPASSWORD"], ConfigurationManager.AppSettings["RSSERVER_NAME"]); rs.Credentials = cred; rs.LogonUser(cred.UserName, cred.Password, cred.Domain); if (rs.CheckAuthenticated()) { // Update policies to the Report Server database rs.SetPolicies(Session["Path"].ToString(), newPolicy); if (dbroundtrip == true) { gvListChildren.DataSource = rs.ListChildren("/", true); gvListChildren.DataBind(); } } } catch { if (rs != null) { rs.Dispose(); } throw; } if (rs != null) { rs.Dispose(); } } catch { throw; } }