首页 > 解决方案 > SSRS 表单身份验证 - 如何将 Cookie 凭据传递到报表服务器

问题描述

我目前正在尝试使用表单身份验证在我的 Web 应用程序中呈现 SSRS 报告。

我的 SSRS 报告版本是 2016 年。

最初我的印象是 NetworkCredentials 可以工作,在遇到错误后,我发现我们需要使用 FormsAuthentication,并传递 cookie 作为对用户进行身份验证的一种手段。

我已按照以下链接中的指南对报告服务器中的配置文件进行了必要的设置:-

https://github.com/Microsoft/Reporting-Services/tree/master/CustomSecuritySample2016

报告服务在 localhost/ReportServer 和 SSRS 门户 localhost/Reports 上按预期工作。我还可以远程访问所述服务器。

下面是我用来获取经过身份验证的 cookie 的代码。

MyReportingService rsClient = new MyReportingService();
rsClient.Url = "http://xxx.xxx.xxx.xxx/reportserver/ReportService2010.asmx";
try
{
     rsClient.LogonUser("user", "password", "");
     Cookie myAuthCookie = rsClient.AuthCookie;
     HttpCookie cookie = new HttpCookie(myAuthCookie.Name, myAuthCookie.Value);
     Response.Cookies.Add(cookie);
}

据推测,这将用于对用户进行身份验证。

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

authCookie.Domain = "DomainName";

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

rvSiteMapping.ServerReport.Cookies.Add(authCookie);

在我的 IReportsServerCredentials 类中的表单身份验证中:-

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

我遇到的问题是应用程序将凭据传递给报表服务器时。我相信我一定是做错了这部分,因为虽然我的应用程序确实获得了 cookie,但当它验证 cookie 提供的凭据时,我收到 text/html 错误:-

Object moved to <a href="/ReportServer/logon.aspx?ReturnUrl=%2fReportserver%2fReportExecution2005.asmx" />

此错误是为了响应在 HttpContext.Current.User = null 的情况下设置默认的通用标识。

if (HttpContext.Current != null
                 && HttpContext.Current.User != null)
{
     userIdentity = HttpContext.Current.User.Identity;
}
else
{
     userIdentity = new GenericIdentity("AnonymousUser");
} 

我试过用谷歌搜索答案,但大多数结果都是针对 Windows 身份验证的,少数与表单身份验证相关的结果与我提到的代码非常相似。

标签: c#reporting-servicesforms-authenticationreporting-services-2016

解决方案


这个问题的根本原因一直在我的眼皮底下。

域名应指 Web 域,而不是 Active Directory 域。

authCookie.Domain = "DomainName";

cookie 现在能够按预期对用户进行身份验证。

希望这可以帮助任何碰巧犯同样错误的人。


推荐阅读