gpt4 book ai didi

c# - Asp.net Web api2 模拟 webClient 调用

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:39 25 4
gpt4 key购买 nike

我们有一个 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 的情况下代表用户强制进行身份验证?在 web.config/IIS 站点中。

我不希望整个代码执行只有这一小部分模拟用户请求...

我确实尝试使用 httpClient,因为我发现自从 httpclient 在新线程中启动以来,它将始终使用应用程序池标识。

我可以自己创建协商电话并将其添加到请求中吗?

谢谢。

编辑:我已经尝试删除除 Kerberos 之外的所有 AuthenticationManager,并且请求仍然使用 NTLM 进行身份验证,我做错了什么?

最佳答案

有多种因素可以使模拟或准确地委托(delegate)用户凭据变得不可能。

1) 如果您使用异步方法(直接或不直接),您可能会遇到身份流动问题。您可以检查以下调用是否有问题:

 System.Security.SecurityContext.IsWindowsIdentityFlowSuppressed();

这应该返回 false - 如果不是,您可以将其用作引用:Calling an async WCF Service while being impersonated

2) 您必须为执行账户启用约束委派。您有一个所谓的 Kerberos 双跃点方案。您必须允许 Sharepoint 用户充当另一个用户,否则 impersonate() 将不会按预期成功。

关于c# - Asp.net Web api2 模拟 webClient 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28764933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com