首页 > 解决方案 > 从 RDL 调用 Web api 时获取“System.Security.SecurityException”

问题描述

我需要从 SSRS 中托管的报告项目中存在的 RDL 文件调用 Web API。RDL 报告中有一个字段将使用 Web Api 响应。我已经在 RDL 中编写了代码来调用该 api 并使用 System.net 库获取响应,但由于某些 System.Web.permission 错误而失败。

我尝试使用以下用 VB.NET(目标框架版本 2.0)编写的代码来使用 Web api。我也尝试trustlevel = fullweb.config中进行设置,但这不起作用并且仍然出现权限被拒绝错误。我在httpWebRequest控制台应用程序中使用 ..etc 执行相同的代码时获得了成功的 Web api 响应,但是当我尝试从 RDL 调用时它失败了。请让我知道如何修复 System.net 库权限错误。

调用上述函数以使用 web api 响应时出现以下错误:

System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()

代码:

`Public Function STSCall() As String
    Dim url As String = "https://httpbin.org/get?token=8041637103881119"
    Dim cardNumber As String = String.Empty
    Dim httpWebRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
    httpWebRequest.Method = "GET"
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Headers.Add("X-AuthMode", "ServerSimpleAuth")
    httpWebRequest.Headers.Add("X-ServerAuthKey", "STS")
    Dim response = CType(httpWebRequest.GetResponse(), System.Net.HttpWebResponse)

    Dim StreamReader As New System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII)
    cardNumber = StreamReader.ToString()

    Return cardNumber
    End Function`

标签: c#apiweb

解决方案


推荐阅读