SSRS报告身份验证间歇性地工作

时间:2016-04-21 06:20:08

标签: c# reporting-services

我需要从我的MVC 6控制器生成SSRS报告服务器端,我现在正在试图获得稳定的解决方案。我遇到我的c#代码将成功对我的SSRS报告服务器进行一段时间的身份验证,然后突然它会给我401 http响应而不进行任何配置或代码更改,这几乎就像我已经通过身份验证并且连接是持久的,然后在一段时间后它将拒绝我。今天早上,当我再次开始我的测试应用程序时,我在开始获得401响应之前对30个好的地雷进行了认证和生成报告

以下是我的rsreportserver.config文件

的设置
<Authentication>
    <Extension Name="Windows" Type="Microsoft.ReportingServices.Authentication.WindowsAuthentication, Microsoft.ReportingServices.Authorization"/>
</Authentication>

<Authentication>
    <AuthenticationTypes>
        <RSWindowsNTLM/>
    </AuthenticationTypes>
    <RSWindowsExtendedProtectionLevel>Off</RSWindowsExtendedProtectionLevel>
    <RSWindowsExtendedProtectionScenario>Proxy</RSWindowsExtendedProtectionScenario>
    <EnableAuthPersistence>true</EnableAuthPersistence>
</Authentication>

这就是我尝试从c#进行身份验证的方式,如果有更好的方法可以做到这一点请知道

public async Task<byte[]> Generate
        (
        string reportServerUrl,
        string reportData,
        string username,
        string password
        )
    {
        var reportUrl = $@"{reportServerUrl}//ReportServer/Pages/ReportViewer.aspx{reportData}";

        using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(username, password, $@"{reportUrl}/") })
        {
            using (var client = new HttpClient(handler))
            {
                client.BaseAddress = new Uri(reportServerUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Add("Authorization", "NTLM");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = await client.GetAsync(reportUrl);
                if (response.IsSuccessStatusCode)
                {
                    return response.Content.ReadAsByteArrayAsync().Result;
                }
            }
        }

        return null;
    }

2 个答案:

答案 0 :(得分:0)

问题在于我试图通过制作正常的HTTP帖子并将参数与我的凭据一起传递到报表服务器来从我的Web应用程序呈现报表。此方法当然可以用于某些报表服务器配置,但不是从C#呈现SSRS报表的正确方法。 Microsoft已经完成了所有艰苦的工作,并提供了可以使用的Web服务端点。令人困惑的部分是它们实际上提供了2个端点,每个端点都有自己独特的功能。

有关如何执行此操作的分步文章,请阅读How to write a C# wrapper class for the SSRS report service上的CodeProject文章

下面我给出了我所做的快速指南

添加网络参考

[Your report server host name]/ReportServer/ReportExecution2005.asmx 

现在创建一个使用添加的Web引用的类

using System.IO;
using System.Net;
using SsrsWrapper.SsrsReportService;

namespace SsrsWrapper
{
    public class ReportManager
    {
        private readonly ReportExecutionService _reportServerExecutionService;

        public ReportManager(string reportServerWsdlUrl,string username,string password,string domain)
        {
            _reportServerExecutionService = new ReportExecutionService
            {
                Url = reportServerWsdlUrl,
                Credentials = new NetworkCredential(username, password, domain)
            };
        }

        public byte[] Render(string reportDirectory,string reportName,string reportFormat,ParameterValue[] parameters)
        {
            _reportServerExecutionService.ExecutionHeaderValue = new ExecutionHeader();

            _reportServerExecutionService.SetExecutionParameters(parameters, "en-us");

            string encoding;
            string mimeType;
            string extension;
            Warning[] warnings;
            string[] streamIds;

            var result = _reportServerExecutionService.Render(reportFormat, @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>", out extension, out encoding, out mimeType, out warnings, out streamIds);

            return result;
        }

        public void Render(string reportDirectory,string reportName,string reportFormat,ParameterValue[] parameters,string destinationPath)
        {
            var result = Render(reportDirectory, reportName, reportFormat, parameters);

            var stream = File.Create(destinationPath, result.Length);

            stream.Write(result, 0, result.Length);
            stream.Close();
        }
    }
}

答案 1 :(得分:-1)

添加[授权]合同标记可以解决您的问题。见下文:

[Authorize]
public class ReportController : Controller
{

    public ActionResult RunReport()
    {

        return View();
    }
}
相关问题