Managing Security > User Context for Multi-Tenancy |
If you use ActiveReports, you can set UserContext attribute in your existing Page Report, RDL report or Section report and uload the report on ActiveReports 8 Server to provide information that is specific to the user that is logged into the server. You can set UserContext attribute as a value or in a connection string. Using this feature you can filter your report data depending on the user roles and attributes that you set. UserContext attribute is particularly helpful when you have multiple tenants who are logged on to the same portal and you want to restrict or filter the data available to each tenant.
UserContext attribute can be set as a value in reports created in ActiveReports. These reports can then be uploaded on the ActiveReports 8 Server to view the output based on the UserContext attribute value that has been set. For example, if ActiveDirectory is set as a Security Provider on the Server, displayName securiy token can be used as a UserContext object.
Following steps will guide you through the procedure to set UserContext values in the Value property of a control.
Caution: Make sure the security token specified for the UserContext object is consistent with your Sercurity Provider settings. |
Expression |
Copy Code
|
---|---|
=Code.UserContext.GetValue("Your_Security_Token")
|
Script |
Copy Code
|
---|---|
public void ActiveReport_ReportStart() {TextBox1.Text = UserContext.GetValue("Your_Security_Token"); } |
You can use any UserContext attribute in a connection string for reports created using ActiveReports. This is useful when each tenant in a multi-tenant application has a separate database, and you need to supply a dynamic value for the database.
In order to implement UserContext in a connection string, it is necessary to first complete your connection settings that is, connecting your report to an appropriate datasource, creating a data set and then re-placing datasource connection string with an expression having a Usercontext attribute. This is necessary because the ActiveReports does not have events to execute script code used for setting UserContext before data request.
Connection String |
Copy Code
|
---|---|
"data source=ServerName;initial catalog=" + Code.UserContext.GetValue("Your_Security_Token") + ";user id=UserName;password=ServerPassword;" |
Script |
Copy Code
|
---|---|
public void ActiveReport_DataInitialize() { var _ds = rpt.DataSource as GrapeCity.ActiveReports.Data.SqlDBDataSource; _ds.ConnectionString = "data source=ServerName;initial catalog=" + UserContext.GetValue("Your_Security_Token") + ";persist security info=False;user id=UserName;password=ServerPassword"; } |
UserContext attribute can be set as a Value for a Parameter to retrieve specific information for the user that is logged into the server. Following steps will guide you through the procedure to set UserContext attribute as a Value for Parameters.
Expression |
Copy Code
|
---|---|
=Code.UserContext.GetValue("Your_Security_Token")
|
Note: These steps assume that a Section Report is already bound to a DataSource. See Bind Reports to a Data Source for details. |
Script |
Copy Code
|
---|---|
public void ActiveReport_ReportStart() { report1.Parameters["paramName"].Value = UserContext.GetValue("Your_Security_Token"); GrapeCity.ActiveReports.Data.SqlDBDataSource ds = (GrapeCity.ActiveReports.Data.SqlDBDataSource) report1.DataSource; ds.SQL = "Select * from TableName where FieldName ='" + rpt.Parameters["paramName"].Value + "'"; } |