Partilhar via


Security Extension Sample

The CustomSecurity sample security extension uses Forms Authentication along with SQL Server to provide a custom security model that works with Reporting Services. This sample is not supported on Itanium-based operating systems.

The SQL Server samples are not installed automatically during setup. For instructions about how to install the samples, see Installing Samples.

ms160724.security(en-US,SQL.90).gifSecurity Note:
The security extension sample should not be deployed and tested in a production environment. Reverting back to Windows Authentication after migrating to a different security extension is generally not recommended. If you do, you may experience errors when you attempt to access items in the report server database that have custom security descriptors, but no Windows Authentication security descriptors. To revert, you will have to reinstall Reporting Services and manually re-apply any role-based security for your Windows users. Before using this sample, you should backup your configuration files.

Important

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.

Requirements

You should be familiar with Visual Studio and either Visual C# or Visual Basic and you must have the following applications installed to run the CustomSecurity sample:

  • Microsoft Visual Studio 2005 or compatible development environment (for viewing the project files).
  • Microsoft .NET Framework version 2.0.
  • SQL Server 2005, including Reporting Services.
  • Reporting Services samples.
  • A report server that you have permission to access on your network, if you plan to use the sample extension to add additional data processing functionality to your server.

Location

This sample is located by default in the Extension Samples\FormsAuthentication Sample subdirectory of the Reporting Services samples directory:

C:\Program Files\Microsoft SQL Server\90\Samples\Reporting Services\Extension Samples\FormsAuthentication Sample

Building the Sample

You must first compile and install the extension. The procedure assumes that you have installed Reporting Services to the default location: C:\Program Files\Microsoft SQL Server\MSSQL.3\Reporting Services. This location will be referred to throughout the remainder of this topic as <install>.

If you have not already created a strong name key file, generate the key file using the following instructions.

To generate a strong name key file

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

    -- or --

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. Use the change directory command (CD) to change the current directory of the command prompt window to the folder where the samples are installed.

    Note

    To determine the folder where samples are located, click the Start button, point to All Programs, point to Microsoft SQL Server, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\100\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k SampleKey.snk

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

To compile the sample using Visual Studio 2005

  1. Open CustomSecurity.sln in Microsoft Visual Studio 2005. If you installed the sample to the default location, you can access it at C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\Samples\Extensions.

  2. In Solution Explorer, select the CustomSecurity project.

  3. On the Project menu, click Add Reference.

    The Add References dialog box opens.

  4. Click the .NET tab.

  5. Click Browse, and find Microsoft.ReportingServices.Interfaces on your local drive. By default, the assembly is located in the <install>\ReportServer\bin directory. Click OK.

    The selected reference is added to your project.

  6. On the Build menu, click Build Solution.

Deploying the Sample

After the sample is compiled, you must copy the DLLs and the ASPX pages to the appropriate subdirectories for your Report Server installation.

To deploy the sample

  1. Copy Microsoft.Samples.ReportingServices.CustomSecurity.dll and Microsoft.Samples.ReportingServices.CustomSecurity.pdb to the <install>\ReportServer\bin directory.

  2. Copy Microsoft.Samples.ReportingServices.CustomSecurity.dll and Microsoft.Samples.ReportingServices.CustomSecurity.pdb to the <install>\ReportManager\bin directory.

  3. Copy the Logon.aspx page to the <install>\ReportServer directory and copy the UILogon.aspx page to the <install>\ReportManager\Pages directory.

After the assembly and logon pages are copied to the server, you need to make some modifications to the Report Server and Report Manager configuration files.

Important

Make backup copies of al of your configuration files before making any changes.

To modify the RSReportServer.config file

  1. Open the RSReportServer.config file with Visual Studio 2005 or a simple text editor such as Notepad. RSReportServer.config is located in the <install>\ReportServer directory.

  2. Locate the <Security> and <Authentication> elements and modify the settings as follows:

    <Security>
       <Extension Name="Forms" 
    Type="Microsoft.Samples.ReportingServices.CustomSecurity.Authorization, 
    Microsoft.Samples.ReportingServices.CustomSecurity" >
          <Configuration>
             <AdminConfiguration>
                <UserName>username</UserName>
             </AdminConfiguration>
          </Configuration>
       </Extension>
    </Security>
    <Authentication>
       <Extension Name="Forms" 
    Type="Microsoft.Samples.ReportingServices.CustomSecurity.AuthenticationExtension,
     Microsoft.Samples.ReportingServices.CustomSecurity" />
    </Authentication>
    

    For more information regarding .NET Framework security and Reporting Services, see Understanding Code Access Security in Reporting Services.

To modify the RSWebApplication.config file

  1. Next, you need to open the Report Manager configuration file, RSWebApplication.config, located in the <install>\ReportManager directory.

  2. Locate the <UI> element and update it as follows:

    <UI>
       <CustomAuthenticationUI>
          <loginUrl>/Pages/UILogon.aspx</loginUrl>
             <UseSSL>True</UseSSL>
       </CustomAuthenticationUI>
       <ReportServerUrl>http://<server>/ReportServer</ReportServerUrl>
    </UI>
    

Note

If you are running the sample security extension in a development environment that does not have a Secure Sockets Layer (SSL) certificate installed, you must change the value of the <UseSSL> element to False in the previous configuration entry. We recommend that you always use SSL when combining Reporting Services with Forms Authentication.

You will need to add a code group for your custom security extension that grants FullTrust permission for your extension. You do this by adding the code group to the rssrvpolicy.config file.

To modify the RSSrvPolicy.config file

  1. Open the rssrvpolicy.config file located the <install>\ReportServer directory.

  2. Locate the existing code group in the security policy file that has a URL membership of $CodeGen as indicated below and then add an entry as follows to the rssrvpolicy.config

    Note

    If you have Analysis Services installed, you will need to change Url="C:\Program Files\Microsoft SQL Server</CODE>MSSQL.2\Reporting Services\ReportServer\bin\Microsoft.Samples.ReportingServices.CustomSecurity.dll to MSSQL.3.

    <CodeGroup
       class="UnionCodeGroup"
       version="1"
       PermissionSetName="FullTrust">
       <IMembershipCondition 
          class="UrlMembershipCondition"
          version="1"
          Url="$CodeGen$/*"
       />
    </CodeGroup>
    <CodeGroup
       class="UnionCodeGroup"
       version="1"
       Name="SecurityExtensionCodeGroup"
       Description="Code group for the sample security extension"
       PermissionSetName="FullTrust">
       <IMembershipCondition 
          class="UrlMembershipCondition"
          version="1"
          Url="C:\Program Files\Microsoft SQL Server\MSSQL.2\Reporting Services\ReportServer\bin\Microsoft.Samples.ReportingServices.CustomSecurity.dll"
       />
    </CodeGroup>
    

Note

For simplicity, the Forms Authentication Sample is weak-named and requires a simple URL membership entry in the security policy files. In your production security extension implementation, you should create strong-named assemblies and use the strong name membership condition when adding security policies for your assembly. For more information about strong-named assemblies, see the Creating and Using Strong-Named Assemblies topic on MSDN.

Next, you will need to increase the permissions for the "My Computer" code group in the Report Manager policy file.

To modify the RSMgrPolicy.config file

  1. Open the Report Manager policy file, rsmgrpolicy.config, located in the <install>\ReportManager directory.

  2. Locate the following code group in rsmgrpolicy.config and change the PermissionSetName attribute from Execution to FullTrust as follows:

    <CodeGroup 
            class="FirstMatchCodeGroup" 
            version="1" 
            PermissionSetName="FullTrust"
            Description="This code group grants MyComputer code Execution 
    permission. ">
        <IMembershipCondition 
                class="ZoneMembershipCondition"
                version="1"
                Zone="MyComputer" />
    

To use Forms Authentication, you need to modify the Web.config files for Report Manager and Report Server to change the authentication mode and disable impersonation.

To modify the Web.config file for Report Server

  1. Open the Web.config file in a text editor. By default, the file is located in the <install>\ReportServer directory.

  2. Locate the <identity> element and set the Impersonate attribute to false.

    <identity impersonate="false" />
    
  3. Locate the <authentication> element and change the Mode attribute to Forms.

  4. Add the following <forms> element as a child of the <authentication> element and set the loginUrl, name, timeout, and path attributes as follows:

    <authentication mode="Forms">
       <forms loginUrl="logon.aspx" name="sqlAuthCookie" timeout="60" 
                   path="/"></forms>
       </authentication>
    
  5. Add the following <authorization> element directly after the <authentication> element.

    <authorization> 
       <deny users="?" />
    </authorization>
    

    This will deny unauthenticated users the right to access the report server. The previously established loginUrl attribute of the <authentication> element will redirect unauthenticated requests to the Logon.aspx page.

To modify the Web.config file for Report Manager

  1. Open the Web.config for Report Manager. It is located in the <install>\ReportManager directory.

  2. Disable impersonation by locating the section <identity impersonate= "true" /> and changing it to the following: <identity impersonate="false" />.

Configuring Anonymous Authentication

By default, the Windows user group Guests includes the IUSR_computername account. This account is used to initially log on locally and view the Logon.aspx page. To support Forms Authentication, you must enable anonymous access for the ReportServer virtual directory. By default, anonymous access is disabled.

To enable anonymous authentication

  1. In Internet Information Services, select the ReportServer virtual directory, which is usually a member of the Default Web site, and open its Property tabs.

  2. Click the Directory Security tab.

  3. In the Anonymous access and authentication control section, click Edit.

    The Authentication Methods dialog box appears.

  4. Select the Anonymous access check box.

  5. Click OK.

    Repeat the above steps for the Reports virtual directory.

Creating the UserAccounts Database

The sample includes a database script, createuserstore.sql, that enables you to set up a user store for the Forms sample in a SQL Server database.

To create the UserAccounts database

  1. Open SQL Server Management Studio, and then connect to your local instance of SQL Server.

  2. Locate the createuserstore.sql SQL script file. The script file is contained within the sample project files.

  3. Locate "LocalMachine" towards the end of the script and replace it with your own computer name. For Windows 2003 users, replace LocalMachine\ASPNET with NT AUTHORITY\NETWORK SERVICE (except when in IIS 5 compatibility mode).

  4. Run the query to create the UserAccounts database.

  5. Exit SQL Server Management Studio.

Testing the Sample

The following procedure tests the sample extension. You will register an administrator user, which adds the user name, password hash, and salt value to the users table in the UserAccounts database. It also will require you to enter that user name in the Report Server configuration file. You will then log on the same user to ensure the correct operation of the password verification routines as well as the proper loading of the extension assembly by the report server.

To create the report project

  1. Restart IIS by running Iisreset.exe at the command prompt.

  2. Open Report Manager. You can do this from the Reporting Services program menu or by accessing the Reports virtual directory from your browser.

  3. Enter a user name and password and click Register User to add the user to the accounts database.

  4. Open the RSReportServer.config file. Locate the <Security> element and add the previously registered user name as follows:

    <Security>
       <Extension Name="Forms" 
    Type="Microsoft.Samples.ReportingServices.CustomSecurity.Authorization, 
    Microsoft.Samples.ReportingServices.CustomSecurity" >
          <Configuration>
             <AdminConfiguration>
                <UserName>username</UserName>
             </AdminConfiguration>
          </Configuration>
       </Extension>
    </Security>
    
  5. Return to the UILogon.aspx page, re-enter the user name and password, and then click Logon.

You should have access to Report Manager and the report server with no restrictions. The administrator user that you create has equivalent permissions on the report server to those of a built-in administrator account on the local computer. For the purpose of this sample, you can only have one user designated as an administrator. Once you have a built-in administrator account, you can register additional users and assign them roles on the report server.

Note

You should add your administrator user to the official System Administrator and Content Manager (root folder) roles of your report server. This prevents empty security descriptors from existing in the report server database. For more information about the System Administrator and Content Manager roles, see Predefined Roles Overview

Using the Web Service with Custom Security

You can use the Web service API with Forms Authentication just as you would with Windows Authentication. However, you must call LogonUser in your Web service code and pass the credentials of the current user. In addition, your Web service client will not have the benefit of automatic cookie management, which is provided by Internet Explorer or other Web browsers. You will have to extend the Microsoft.ReportingServices proxy class to include cookie management. This can be done by overriding the GetWebRequest and GetWebResponse methods of the Web service class.

Debugging the Sample Extension

Running the sample extension in the debugger is not only a great way to troubleshoot difficulties you may have, but it is also an effective way to step through the code and see the report server authentication and authorization process as it is happening.

The Microsoft .NET Framework provides several debugging tools that can help you analyze the sample code. The following procedure uses Visual Studio 2005 to debug the previous sample.

To debug the Forms Authentication sample code

  1. Start Visual Studio and open CustomSecurity.sln on your test report server.

  2. Open Internet Explorer and navigate to Report Manager while leaving the sample code open in Visual Studio.

  3. Switch to Visual Studio and set some break points in the custom security extension project code.

  4. With the extension project still the active window, from the Debug menu, click Process.

    The Processes dialog opens.

  5. From the list of processes, select the Aspnet_wp.exe process (or W3wp.exe, if your application is deployed on IIS 6.0), and click Attach.

  6. In the Attach to Process dialog, select the Common Language Runtime program type, and then click OK. For improved debugging performance, make sure that Native is not a selected program type.

  7. When the sample runs, a logon form appears. Type the user credentials into the logon form and click the Logon button.

    Whenever your break points are encountered during processing, the debugger should stop execution at that point.

  8. Step through your code using the F11 key. For more information about using Visual Studio for debugging, see your Visual Studio 2005 documentation.

Note

Debugging this way requires a lot of resources and processor time. If you run into difficulties, close Visual Studio, reset IIS, and begin again by attaching the CustomSecurity solution to the ASP.NET worker process and logging on to Report Manager.

Removing the Sample Extension

While not generally recommended, it is possible to revert back to Windows Authentication after you have tried out the sample.

To revert to Windows security

  1. Restore the following files from your backup copies: Web.config, RSReportServer.config, and RSWebApplication.config. This should set the authentication and authorization methods for the report server to the default Windows security. This should also remove any entries you made for your extension in the Report Server or Report Manager configuration files.

  2. Disable anonymous access in Internet Information Services (IIS) for the report server virtual directory.

After the configuration information is removed, your security extension is no longer available to the report server. You should not have to remove any security descriptors that were created while you were running the report server under the sample security extension. The report server automatically assigns the System Administrator role to the BUILTIN\Administrators group on the computer hosting the report server when Windows Authentication is enabled. However, you will have to manually re-apply any role-based security for your Windows users.

Note that reverting back to Windows Authentication after migrating to a different security extension is generally not recommended. If you do, you may experience errors when you attempt to access items in the report server database that have custom security descriptors, but no Windows Authentication security descriptors.

See Also

Tasks

SQL Server Reporting Services Samples

Other Resources

Extension Samples (Reporting Services)
Implementing a Security Extension

Help and Information

Getting SQL Server 2005 Assistance

Change History

Release History

5 December 2005

Changed content:
  • Changed instructions for generating a key file, including the name and location of the key file.