domingo, 17 de junio de 2012

Autentificación Reporting Services en VB.NET

Enunciado

Construir una clase para autentificarse ante un servidor de informes de Microsoft (SSRS) puede ser útil para:
  1. Utilizar el visor de informes con Informes de servidor (processingMode=Remote)
  2. Utilizar servicios web para Cargar y modificar la definición de informes

Solución

Crearemos la siguiente clase:>

Imports System.Net
Imports System.Security.Principal
Imports Microsoft.Reporting.WebForms
 _
Public NotInheritable Class MyReportServerCredentials
    Implements IReportServerCredentials

    Public ReadOnly Property ImpersonationUser() As WindowsIdentity _
            Implements IReportServerCredentials.ImpersonationUser
        Get

            'Use the default windows user.  Credentials will be
            'provided by the NetworkCredentials property.
            Return Nothing

        End Get
    End Property

    Public ReadOnly Property NetworkCredentials() As ICredentials _
            Implements IReportServerCredentials.NetworkCredentials
        Get

            'Read the user information from the web.config file.  
            'By reading the information on demand instead of storing 
            'it, the credentials will not be stored in session, 
            'reducing the vulnerable surface area to the web.config 
            'file, which can be secured with an ACL.

            'User name
            Dim userName As String = My.Settings.usuario

            If (String.IsNullOrEmpty(userName)) Then
                Throw New Exception("Missing user name from web.config file")
            End If

            'Password
            Dim password As String = My.Settings.password

            If (String.IsNullOrEmpty(password)) Then
                Throw New Exception("Missing password from web.config file")
            End If

            'Domain
            Dim domain As String = My.Settings.dominio

            If (String.IsNullOrEmpty(domain)) Then
                Throw New Exception("Missing domain from web.config file")
            End If

            Return New NetworkCredential(userName, password, domain)

        End Get
    End Property

    Public Function GetFormsCredentials(ByRef authCookie As Cookie, _
                                        ByRef userName As String, _
                                        ByRef password As String, _
                                        ByRef authority As String) _
                                        As Boolean _
            Implements IReportServerCredentials.GetFormsCredentials

        authCookie = Nothing
        userName = Nothing
        password = Nothing
        authority = Nothing

        'Not using form credentials
        Return False

    End Function

End Class

Para usarla en el visor de informes:

Protected Sub Page_Init(ByVal sender As Object, _
                              ByVal e As System.EventArgs) _
                              Handles Me.Init
        Visor.ServerReport.ReportServerUrl = New Uri(My.Settings.SSRS)
        Visor.ServerReport.ReportServerCredentials = _
            New MyReportServerCredentials()
    End Sub

Para usarla en los web Services

        Dim SSRS As New ReportingService2010
        SSRS.Credentials = (New MyReportServerCredentials).NetworkCredentials
        Dim informe = SSRS.GetItemDefinition(My.Settings.mapas + nombre)
        SSRS = Nothing

No hay comentarios: