【问题标题】:Asp.net Web api2 impersonate on webClient callAsp.net Web api2 模拟 webClient 调用
【发布时间】: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


【解决方案1】:

有多种因素可能会导致无法进行模拟,或者更准确地说是无法授权用户凭据。

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

 System.Security.SecurityContext.IsWindowsIdentityFlowSuppressed();

这应该返回 false - 如果没有,您可以将其用作参考:Calling an async WCF Service while being impersonated

2) 您必须为执行帐户启用约束委派。您有一个所谓的 Kerberos 双跳方案。您必须允许 Sharepoint 用户充当其他用户,否则 impersonate() 将无法按预期成功。

【讨论】:

  • System.Security.SecurityContext.IsWindowsIdentityFlowSuppressed();在 impersonation() 前后返回 false
  • 这是一个系统范围的设置,可能会阻止模拟,它应该是假的。但是您仍然需要委托权限。
猜你喜欢
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
  • 2022-10-24
  • 2015-03-04
  • 2011-08-30
  • 2017-11-02
相关资源
最近更新 更多