在 C# 中使用 WebRequest 时为 Microsoft Report Server 提供凭据
我想使用我的 C# 代码从我们的 Microsoft Report Server 下载 PDF 报告。我不知道为什么,但我做错了什么。我总是收到身份验证失败的错误(HTTP 401)。
public static async Task<Stream> DownloadWebDocument(string url) {
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException(nameof(url));
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
request.Credentials = new NetworkCredential("MyUsername", "MyPassword", "MyDomain");
//request.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("MyUsername:MyPassword"))}");
try {
using WebResponse response = await request.GetResponseAsync();
return response.GetResponseStream();
} catch (Exception ex) {
var a = ex.Message;
throw;
}
//return await DownloadWebDocument(uri);
}
这段代码总是会遇到异常。但为什么?
PS:
根据要求,这是堆栈跟踪。没有内在的例外。
bei System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
bei System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei DE.ZA.TrailerLoadingAssistant.Web.Code.WebHelper.<DownloadWebDocument>d__0.MoveNext() in C:\Users\Reichelt\source\repos\DE.ZA.TrailerLoading\DE.ZA.TrailerLoadingAssistant.Web\Code\WebHelper.cs:Zeile 28.
I would like to download a PDF report from our Microsoft Report Server using my C# code. I don't know why, but I' m doing something wrong. I always get an error back that the authentication failed (HTTP 401).
public static async Task<Stream> DownloadWebDocument(string url) {
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException(nameof(url));
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
request.Credentials = new NetworkCredential("MyUsername", "MyPassword", "MyDomain");
//request.Headers.Add("Authorization", quot;Basic {Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("MyUsername:MyPassword"))}");
try {
using WebResponse response = await request.GetResponseAsync();
return response.GetResponseStream();
} catch (Exception ex) {
var a = ex.Message;
throw;
}
//return await DownloadWebDocument(uri);
}
This code always runs into the exception. But why?
PS:
As requested, here's the stack trace. There is no inner exception.
bei System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
bei System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei DE.ZA.TrailerLoadingAssistant.Web.Code.WebHelper.<DownloadWebDocument>d__0.MoveNext() in C:\Users\Reichelt\source\repos\DE.ZA.TrailerLoading\DE.ZA.TrailerLoadingAssistant.Web\Code\WebHelper.cs:Zeile 28.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上我使用不同的方法将我的报告呈现为 PDF 文档:
Actually I use different method to render my report as PDF document:
我遇到过这个问题,似乎有人正在更改网络权限。一周使用 Credentials 可以工作,然后下周使用 DefaultCredentials 就可以工作。真的很奇怪,所以我放入了 Try/Catch 并在服务帐户失败时诉诸 DefaultCredentials,请参阅代码注释:
I've had this problem and it seemed someone was changing the network permissions. One week using Credentials would work then the next week using DefaultCredentials worked. Really strange, so I put in a Try/Catch and resort to the DefaultCredentials if the Service Account fails, see the code comment: