IReportServerCredentials
我们就要先利用这个接口创建一个类,然后类中实现这个接口的成员。IReportServerCredentials接口主要是两个成员属性(ImpersonateUser和NetworkCredentials)和一个成员方法(GetFormCredentials)。
ImpersonateUser 在我们这儿没什么多大用处,所以直接return null。NetworkCredentials就是我们需要返回的,所以return new NetworkCredential(_username, _password, _domain)。
那么_username、_password、_domain哪儿来的呢,类里面放三个Private的变量,然后通过类的初始化方法传进去就行了啊。
下面是类的源代码和调用类的源代码:
/********************************************************************************
MyReportViewerCredential类
********************************************************************************/
using System;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Web.SessionState;
using System.Security.Principal;
public class ReportViewerCredentials
{
public class MyReportViewerCredential : IReportServerCredentials
{
private string _username;
private string _password;
private string _domain;
public Uri ReportServerUrl;
public MyReportViewerCredential(string username, string password, string domain)
{
_username = username;
_password = password;
_domain = domain;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}
public System.Net.ICredentials NetworkCredentials
{
get
{
return new NetworkCredential(_username, _password, _domain);
}
}
public bool GetFormsCredentials(out Cookie authCookie,
out string user, out string password, out string authority)
{
authCookie = null;
user = _username;
password = _password;
authority = _domain;
return false;
}
}
}
/********************************************************************************
调用MyReportViewerCredential类
********************************************************************************/
protected void TreeViewNavigator_SelectedNodeChanged(object sender, EventArgs e)
{
ReportViewerCredentials.MyReportViewerCredential rvc = new ReportViewerCredentials.MyReportViewerCredential
(Session["UserName"].ToString(), Session["Password"].ToString(), Session["Domain"].ToString());
ReportViewerMain.ServerReport.ReportServerCredentials = rvc;
ReportViewerMain.ServerReport.ReportPath = TreeViewNavigator.SelectedNode.Value.ToString();
}