IReportServerCredentials 介面
可讓物件提供用來連接到報表伺服器的認證。
命名空間: Microsoft.Reporting.WebForms
組件: Microsoft.ReportViewer.WebForms (在 microsoft.reportviewer.webforms.dll)
語法
'宣告
Public Interface IReportServerCredentials
'用途
Dim instance As IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
public interface IReportServerCredentials
public interface IReportServerCredentials
範例
下列程式碼範例顯示 IReportServerCredentials 的實作。這個程式碼包括實際的實作、提供 Cookie 支援的公用程式類別,以及顯示如何呼叫自訂驗證的範例頁面。
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.
}
}
下列程式碼會衍生 ReportExecutionService Web 服務的子類別來提供驗證 Cookie。
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;
}
}
下列程式碼顯示如何使用自訂驗證。
<%@ 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>
另請參閱
參考
IReportServerCredentials 成員
Microsoft.Reporting.WebForms 命名空間