Partager via


Interface IReportServerCredentials

Permet aux objets de fournir les informations d'identification à utiliser pour la connexion à un serveur de rapports.

Espace de noms: Microsoft.Reporting.WebForms
Assembly: Microsoft.ReportViewer.WebForms (dans microsoft.reportviewer.webforms.dll)

Syntaxe

'Déclaration
Public Interface IReportServerCredentials
'Utilisation
Dim instance As IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
public interface IReportServerCredentials
public interface IReportServerCredentials

Exemple

Les exemples de code suivants montrent une implémentation de IReportServerCredentials. Le code inclut l'implémentation réelle, une classe utilitaire qui fournit la prise en charge des cookies et un exemple de page illustrant le mode d'appel de l'authentification personnalisée.

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.
    }
}

Dans le code ci-dessous, le service Web ReportExecutionService correspond à une sous-classe, de façon à fournir un cookie d'authentification.

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;
    }
}

Le code suivant montre comment utiliser l'authentification personnalisée.

<%@ 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>

Voir aussi

Référence

Membres IReportServerCredentials
Espace de noms Microsoft.Reporting.WebForms