Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟、Active Directory和;用户无权访问xxxx";问题_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3_Impersonation - Fatal编程技术网

C# 模拟、Active Directory和;用户无权访问xxxx";问题

C# 模拟、Active Directory和;用户无权访问xxxx";问题,c#,asp.net,asp.net-mvc,asp.net-mvc-3,impersonation,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Impersonation,我有两个ASP.NET MVC 3应用程序。我正在通过web.config使用模拟来查询Active Directory以获取用户的详细信息。应用程序使用Windows身份验证,不允许匿名用户。一个应用程序是用户执行任务的主要应用程序。另一个允许用户设置其他用户的应用程序,使其与应用程序1中的应用程序相似 测试用户的帐户出现以下错误: SQL1092N "<DOMAIN ID>" does not have the authority to perform the requeste

我有两个ASP.NET MVC 3应用程序。我正在通过web.config使用模拟来查询Active Directory以获取用户的详细信息。应用程序使用Windows身份验证,不允许匿名用户。一个应用程序是用户执行任务的主要应用程序。另一个允许用户设置其他用户的应用程序,使其与应用程序1中的应用程序相似

测试用户的帐户出现以下错误:

SQL1092N  "<DOMAIN ID>" does not have the authority to perform the requested command.
正是在这个web请求发生之后,主应用程序尝试访问数据库,现在看来上面的调用已经撤消了对应用程序的模拟,因此用户尝试打开数据库连接的任何操作都失败了。至少,这是我在一天的痛击之后的工作原理

我的问题是,如何让应用程序的模拟恢复到web.config中的用户?或者,在发出web请求时,是否有方法确保模拟上下文仅适用于该请求

所有这一切的关键是,第二个应用程序有自己的sql server数据库。主应用程序使用DB2。我想写一次数据库访问代码,但在两个应用程序中都使用它。目前我就是这么做的,但我依靠web请求获取数据的方法可能不是最好的方法


我愿意接受任何想法、评论、建议和/或批评。我应该如何处理这个问题呢?

好吧……我的理论是,当发出web请求时,IPrincipal上下文发生了变化,这证明是正确的,这使得修复非常容易。最好的部分是,我可以继续使用我构建的api来发出此请求,而无需重复Sql Server实体框架部分

我对api库进行了以下调用:

            proxyRequestResultDetails = ProxyApiWrapper.GetProxies(
                adUserInfo.AssociateId,
                context.User);
授权筛选器属性正在调用此代码。方法原型看起来像

public void OnAuthorization(AuthorizationContext filterContext)     
在内部,该调用使GetProxies方法执行以下调用:

        public static StreamReader GetWebRequestStream(
             string url,
             string contentType,
             bool useDefaultCredentials,
             IPrincipal user)
        {

            var impersonationContext = ((WindowsIdentity)user.Identity).Impersonate();            
            var request = WebRequest.Create(url);

            try
            {
                request.ContentType = contentType;
                //request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                //request.UseDefaultCredentials = useDefaultCredentials;            
                //IWebProxy p = new WebProxy();
                //request.Proxy = p.
                request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
                request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                var response = (HttpWebResponse)request.GetResponse();
                return new StreamReader(response.GetResponseStream());
            }
            catch (Exception e)
            {
                impersonationContext.Undo();
                throw e;
            }
            finally
            {
                impersonationContext.Undo();
            }

        }
当调用方法返回时,用户的身份不再是为应用程序设置的身份 冒充。解决方法非常简单:

            //Track current identity before proxy call
            IPrincipal user = context.User;
            proxyRequestResultDetails = ProxyApiWrapper.GetProxies(
                adUserInfo.AssociateId,
                context.User);

            //Undo any impersonating done in the GetProxies call
            context.User = user;    
两行代码解决了12小时的头痛问题。情况可能更糟。无论如何谢谢你做我的传声筒。我试过了
对鸭子进行这种转换,但是鸭子感到困惑。

有什么我需要澄清的吗?这个问题的措辞会妨碍你吗?欢迎反馈。我不介意在可能的情况下重新措辞或澄清。
            //Track current identity before proxy call
            IPrincipal user = context.User;
            proxyRequestResultDetails = ProxyApiWrapper.GetProxies(
                adUserInfo.AssociateId,
                context.User);

            //Undo any impersonating done in the GetProxies call
            context.User = user;