在没改Form表单验证情况下Reporting Service Webservice使用的是Windows身份验证,一旦改为Form之后随之带来的麻烦就是WebService的调用失效了,会报错处理。 这个时候就需要重写几个重要的方法:
/// <summary>
/// 加载ReportService WebService
/// </summary>
private static object Locker = new object();
public class RenderReport : ReportingService2010
{
private string m_authCookieName;
private Cookie m_authCookie;
public RenderReport()
{
// Set the server URL. You can pull this from a config file or what ever way you want to make it dynamic.
base.Url = "http://xxx/ReportServer/ReportService2010.asmx"; //在webconfig里面定义一个Reporting service WebServer的地址。 http://xxx/ReportServer/ReportService2005.asmx
// Calling the LogonUser method defined in the ReportService2005.asmx end point.
// The LogonUser method authenticates the specified user to the Report Server Web Service when custom authentication has been configured.
// This is to authenticate against the FBA code and then store the cookie for future reference.
try
{
base.LogonUser("admin", "123", null);
}
catch (Exception)
{
}
}
/// <summary>
/// Overriding the method defined in the base class. 重写GetWebRequest方法
/// </summary>
/// <param name=”uri”></param>
/// <returns></returns>
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Credentials = base.Credentials;
request.CookieContainer = new CookieContainer();
if (m_authCookie != null
)
{
request.CookieContainer.Add(m_authCookie);
}
return request;
}
/// <summary>
/// Overriding the method defined in the base class. 重写GetWebResponse
/// </summary>
/// <param name=”request”></param>
/// <returns></returns>
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
string cookieName = response.Headers["RSAuthenticationHeader"];
if (cookieName != null)
{
m_authCookieName = cookieName;
HttpWebResponse webResponse = (HttpWebResponse)response;
Cookie authCookie = webResponse.Cookies[cookieName];
// save it for future reference and use.
m_authCookie = authCookie;
}
return response;
}
/// <summary>
/// 先移除拥有此角色的用户对应报表的权限。
/// </summary>
/// <param name="context"></param>
public bool AddUserToReportManage(string UserAccount)
{
try
{
lock (this)
{
//RoleManage_D Role_D = new RoleManage_D();
//string RoleInfoId = context.Request["RoleInfoid"];
//DataTable elemen = Role_D.GetRoleElemen(RoleInfoId); //获取报表
//DataTable user = Role_D.GetRoleUser(RoleInfoId); //获取用户
//if (elemen.Rows.Count <= 0 || user.Rows.Count <= 0) //判断非空
//{
// return;
//}
bool policys = false;
Role rol = new Role();
rol.Description = "浏览";
rol.Name = "Content Manager";
Role[] rols = { rol };
Policy pol = new Policy();
pol.GroupUserName = UserAccount;
pol.Roles = rols;
Policy[] UserPolicy = base.GetPolicies("/", out policys);//先获取根路径所有用户和角色
List<Policy> PolicyList = new List<Policy>(UserPolicy);//把获取到的用户添加到泛型集合里面去
PolicyList.Add(pol);
UserPolicy = PolicyList.ToArray();//重新赋值数组
base.SetPolicies("/", UserPolicy);//给报表重新赋予权限
return true;
}
}
catch (Exception ex)
{
return false;
}
}