【发布时间】:2023-03-31 16:55:01
【问题描述】:
我们有一个 web api 是用户帐户,比如说ApplicationPoolUser
使用的可以访问 api 使用的数据库等工作正常。
但我正在尝试使用webClient 对远程服务器(sharepoint 2007)上的文件发送 http get 方法
这是我使用的:
WindowsImpersonationContext impersonationContext = null;
Uri uri = new Uri(Path.Combine(this.Document.path , Document.fileNameOriginal));
Stream stream = null;
// WindowsIdentity.GetCurrent().Name return 'ApplicationPoolUser'
try
{
WindowsIdentity wi = System.Web.HttpContext.Current.Request.LogonUserIdentity;
impersonationContext = WindowsIdentity.Impersonate(wi.Token);
// WindowsIdentity.GetCurrent().Name return 'CurrentRequestingUser'
WebClient client = new WebClient() {
UseDefaultCredentials = true,
CachePolicy = new System.Net.Cache.RequestCachePolicy(RequestCacheLevel.BypassCache)
};
stream = client.OpenRead(uri);
// OpenRead Authentified on sharepoint server has ApplicationPoolUser
}
catch(WebException ex)
{
HttpWebResponse webResp = (HttpWebResponse)ex.Response;
if(webResp.StatusCode == HttpStatusCode.NotFound)
throw new GeneralException(Common.Enums.ExceptionMessage.NotFound, webResp.StatusDescription);
else
{
throw ex;
}
}
有没有办法在不打开 asp.net Identity ON 的情况下代表用户强制进行身份验证?在 web.config / IIS 站点中。
我不希望执行的整个代码只有这么一小部分模拟用户请求......
我确实尝试使用 httpClient,但我发现由于 httpclient 在新线程中启动,它将始终使用应用程序池标识。
我可以自己创建 Negotiate Call 并将其添加到请求中吗?
谢谢。
编辑: 我已经尝试删除除 Kerberos 之外的所有 AuthenticationManager,并且请求仍然使用 NTLM 进行身份验证,我做错了什么?
【问题讨论】:
-
您不应该在编辑中提出新问题。
标签: c# asp.net webclient dotnet-httpclient