Freigeben über


IReportServerCredentials-Schnittstelle

Ermöglicht Objekten die Bereitstellung von Anmeldeinformationen für die Herstellung einer Verbindung mit einem Berichtsserver.

Namespace: Microsoft.Reporting.WebForms
Assembly: Microsoft.ReportViewer.WebForms (in microsoft.reportviewer.webforms.dll)

Syntax

'Declaration
Public Interface IReportServerCredentials
'Usage
Dim instance As IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
public interface IReportServerCredentials
public interface IReportServerCredentials

Beispiel

In den folgenden Codebeispielen wird eine Implementierung von IReportServerCredentials gezeigt. Der Code beinhaltet die eigentliche Implementierung, eine Dienstprogrammklasse für die Unterstützung von Cookies und eine Beispielseite, die zeigt, wie die benutzerdefinierte Authentifizierung aufgerufen wird.

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

Im nachstehenden Code wird der ReportExecutionService-Webdienst als Unterklasse eingesetzt, um ein Authentifizierungscookie bereitzustellen.

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

Der folgende Code zeigt, wie die benutzerdefinierte Authentifizierung verwendet wird.

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

Siehe auch

Referenz

IReportServerCredentials-Member
Microsoft.Reporting.WebForms-Namespace