使用Microsoft.Reporting. WebForms中ReportViewer报表查看器(Server方式)

添加Web Reference: http://<ReportServerName>/reportserver/reportexecution2005.asmx<?xml:namespace prefix = o />

 

login

        Message.Text = "";

 

        MyReportingService svc = new MyReportingService();

 

        //设置Web Service 的基URL

        svc.Url = "http://<ReportServerName>/reportserver/reportexecution2005.asmx";

        try

        {

            //登录WebService

            //svc.LogonUser(Username.Text, Password.Text, "Kerberos");

            svc.LogonUser(Username.Text, Password.Text, null);

            Cookie myAuthCookie = svc.AuthCookie;

 

            //如果登录后cookie为空,则登陆失败

            if (myAuthCookie == null)

            {

                Message.Text = "Logon failed";

            }

            //如果登录成功,页面转向ReturnUrl,并添加登录cookie

            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;

        }

MyReportingService:

using ReportExecution2005;

 

/// <summary>

/// ReportExecutionService的扩展代理类,

/// </summary>

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;

    }

}

ReportViewDemo:

using System.Web.SessionState;

using System.Security.Principal;

using Microsoft.Reporting.WebForms;

 

/// <summary>

/// 继承IRequiresSessionState,指定目标HTTP处理程序对会话状态具有访问权

/// </summary>

public partial class ReportViewDemo : Page, IRequiresSessionState

{

   

    void Page_Load(object sender, EventArgs e)

    {

        //读取Cookie

        HttpCookie cookie = Request.Cookies["sqlAuthCookie"];

        if (cookie == null)

        {

            string strUrl = "login.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);

            Response.Redirect(strUrl);

        }

        else

        {

            reportViewer.ProcessingMode = ProcessingMode.Remote;

            reportViewer.ServerReport.ReportServerUrl = new Uri("http://<ServerName>/reportserver");

            reportViewer.ServerReport.ReportPath = "/ReportProject1/Report1";

 

            //System.Net.Cookie

            Cookie authCookie = new Cookie(cookie.Name, cookie.Value);

            authCookie.Domain = "localhost";

 

            //报表凭据验证

            reportViewer.ServerReport.ReportServerCredentials = new MyReportServerCredentials(authCookie);

          

        }

    }

}

MyReportServerCredentials :

class MyReportServerCredentials : IReportServerCredentials

{

    private Cookie m_authCookie;

 

    public MyReportServerCredentials(Cookie authCookie)

    {

        m_authCookie = authCookie;

    }

 

    public WindowsIdentity ImpersonationUser

    {

        get

        {

            return null// Use default identity.

        }

    }

 

    //Window集成验证方式

    public ICredentials NetworkCredentials

    {

        get

        {

            //return null;  // Not using NetworkCredentials to authenticate.

            return new System.Net.NetworkCredential("username", "password");

        }

    }

 

    //Form认证

    public bool GetFormsCredentials(out Cookie authCookie,

        out string user, out string password, out string authority)

    {

        //设置返回的cookie

        authCookie = m_authCookie;

        user = password = authority = null;

        return true// Use forms credentials to authenticate.

    }

}

转载于:https://www.cnblogs.com/caoxch/archive/2006/11/12/558277.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值