繁体   English   中英

Windows模拟问题

[英]Windows Impersonation Issue

嗨,大家好,我很难弄清为什么WindowsIdentity中的模拟无法正常工作。

我将在下面介绍这个想法以及代码。

因此,想法是我需要在我的应用程序内访问另一个应用程序API。

public async Task<UserHrtbProfileDTO> HasHrtbAccessAsync(int userId, string systemUser)
        {
            WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            using (identity.Impersonate())
            {
                using (HttpClientHandler handler = new HttpClientHandler
                {
                    Credentials = CredentialCache.DefaultNetworkCredentials,
                    UseDefaultCredentials = true
                })
                {
                    using (HttpClient client = new HttpClient(handler, true))
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        string requestURI = string.Format(ConfigurationManager.AppSettings["HRTB:Profile_Url"], userId);
                        HttpResponseMessage response = await client.GetAsync(requestURI);

                        return response.RequestMessage.RequestUri.OriginalString.Contains(requestURI)
                             ? new UserHrtbProfileDTO
                             {
                                 HrtbProfileUrl = string.Format(ConfigurationManager.AppSettings["HRTB:Profile_Url"], userId),
                                 ResponseURI = response.RequestMessage.RequestUri.AbsoluteUri,
                             }
                             : new UserHrtbProfileDTO
                             {
                                 ResponseURI = response.RequestMessage.RequestUri.AbsoluteUri,
                                 RequestURI = requestURI,
                                 HasHrtbAccess = false,
                                 IdentityUserName = identity.Name + "\n" + identity.IsAuthenticated + "\n" + identity.AuthenticationType + "\n" + identity.ImpersonationLevel + "\n"
                             };
                    }
                }
            }
        }

如果我在本地IIS上运行,则行为正常,我可以在需要的地方进行访问,而在我没有它的地方可以进行访问。 提出请求的API响应返回我期望的响应。

但是,当我从服务器部署并运行时,响应是不同的,因此其行为并非应有的。 我猜测从发出请求的用户的角度来看,该请求不是发出的。

我可以解决问题的任何想法,建议或其他方法。 谢谢你们。

对我而言,它可以从web.config文件启用模拟,并将IIS应用程序池设置为经典托管管道。

如果您对其他解决方案有其他了解,请分享。 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM