Interfaccia IReportServerCredentials

Consente agli oggetti di specificare le credenziali da utilizzare per la connessione a un server di report.

Spazio dei nomi: Microsoft.Reporting.WebForms
Assembly: Microsoft.ReportViewer.WebForms (in microsoft.reportviewer.webforms.dll)

Sintassi

'Dichiarazione
Public Interface IReportServerCredentials
'Utilizzo
Dim instance As IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
public interface IReportServerCredentials
public interface IReportServerCredentials

Esempio

Negli esempi di codice seguenti viene illustrata un'implementazione di IReportServerCredentials. Nel codice sono incluse l'effettiva implementazione, una classe di utilità per il supporto dei cookie e una pagina di esempio in cui viene illustrata la modalità di chiamata dell'autenticazione personalizzata.

using System;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using System.Security.Principal;
using Microsoft.Reporting.WebForms;

public class Demo : Page, IRequiresSessionState
{
    private HtmlGenericControl body;
    private HtmlForm form;
    private ReportViewer reportViewer;

    void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["sqlAuthCookie"];
        if (cookie == null)
        {
            Response.Redirect("/logon.aspx?ReturnUrl=" + 
HttpUtility.UrlEncode(Request.RawUrl));
        }
        else
        {
            body.Attributes.Add("style", "margin:0px");
            this.Controls.Add(body);

            body.Controls.Add(form);

            reportViewer.Width = Unit.Percentage(100);
            reportViewer.Height = Unit.Percentage(100);
            reportViewer.ProcessingMode = ProcessingMode.Remote;
            reportViewer.ServerReport.ReportServerUrl = new Uri("http://myhost/reportserver");
            reportViewer.ServerReport.ReportPath = "/MyReport";

            Cookie authCookie = new Cookie(cookie.Name, cookie.Value); 
            authCookie.Domain = "myhost";
            reportViewer.ServerReport.ReportServerCredentials =
            new MyReportServerCredentials(authCookie);

            form.Controls.Add(reportViewer);
        }
    }

    void Page_Init(object sender, EventArgs e)
    {
        form = new HtmlForm();
        body = new HtmlGenericControl("body");
        reportViewer = new ReportViewer();
    }
}

class MyReportServerCredentials : IReportServerCredentials
{
    private Cookie m_authCookie;

    public MyReportServerCredentials(Cookie authCookie)
    {
        m_authCookie = authCookie;
    }

    public WindowsIdentity ImpersonationUser
    {
        get
        {
            return null;  // Use default identity.
        }
    }

    public ICredentials NetworkCredentials
    {
        get
        {
            return null;  // Not using NetworkCredentials to 
authenticate.
        }
    }
 
    public bool GetFormsCredentials(out Cookie authCookie,
        out string user, out string password, out string authority)
    {
        authCookie = m_authCookie;
        user = password = authority = null;
        return true;  // Use forms credentials to authenticate.
    }
}

Nel codice seguente viene creata una sottoclasse del servizio Web ReportExecutionService per specificare un cookie di autenticazione.

using System;
using System.Net;

// Subclass ReportExecutionService is used in order to extract an
// authenticated cookie from WebResponse.
public class MyReportingService : ReportExecutionService
{
    private Cookie m_authCookie;

    public Cookie AuthCookie
    {
        get
        {
            return m_authCookie;
        }
    }

    protected override WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest request = 
(HttpWebRequest)HttpWebRequest.Create(uri);
        request.Credentials = base.Credentials;
        request.CookieContainer = new CookieContainer();
        if (m_authCookie != null)
            request.CookieContainer.Add(m_authCookie);
        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        string cookieName = response.Headers["RSAuthenticationHeader"];
        if (cookieName != null)
        {
            HttpWebResponse webResponse = (HttpWebResponse)response;
            m_authCookie = webResponse.Cookies[cookieName];
        }
        return response;
    }
}

Nel codice seguente viene illustrato come utilizzare l'autenticazione personalizzata.

<%@ Page Language="C#" Debug="true" AutoEventWireup="True" %>
<%@ Import Namespace="System.Net" %>

<html>
<head>
   <script runat="server">

      void LogonBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text = "";
         MyReportingService svc = new MyReportingService();
         svc.Url = "http://myhost/reportserver/reportexecution2005.asmx";
         try
         {
            svc.LogonUser(Username.Text, Password.Text, null);
            Cookie myAuthCookie = svc.AuthCookie;
            if (myAuthCookie == null)
            {
               Message.Text = "Logon failed";
            }
            else
            {
               HttpCookie cookie = new HttpCookie(myAuthCookie.Name, 
myAuthCookie.Value); 
               Response.Cookies.Add(cookie);
               string returnUrl = Request.QueryString["ReturnUrl"];
               if (returnUrl == null || !returnUrl.StartsWith("/"))
                  Message.Text = "Return url is missing or invalid!";
               else
                  Response.Redirect(HttpUtility.UrlDecode(returnUrl)); 
            }
         }
         catch (Exception ex)
         {
            Message.Text = "Logon failed: " + ex.Message;
         }
      }

   </script>
</head>
<body>
  <form id="form1" runat="server">
    <asp:label id="Message" runat="server"/>
    <table>
      <tr>
        <td><asp:Label ID="Label1" runat="server" Text="User 
Name:"/></td>
        <td><asp:TextBox ID="Username" runat="server"/></td>
      </tr>
      <tr>
        <td><asp:Label ID="Label2" runat="server" 
Text="Password:"/></td>
        <td><asp:TextBox ID="Password" runat="server"/></td>
      </tr>
    </table>
    <asp:Button ID="Button1" runat="server" Text="Logon" 
OnClick="LogonBtn_Click" />
  </form>
</body>
</html>

Vedere anche

Riferimento

Membri IReportServerCredentials
Spazio dei nomi Microsoft.Reporting.WebForms