Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# 如何对ASP.NET MVC站点上的用户使用Windows身份验证,并在后端WCF服务上使用AppPool用户模拟用户?_C#_Asp.net Mvc_Wcf_Windows Authentication_Impersonation - Fatal编程技术网

C# 如何对ASP.NET MVC站点上的用户使用Windows身份验证,并在后端WCF服务上使用AppPool用户模拟用户?

C# 如何对ASP.NET MVC站点上的用户使用Windows身份验证,并在后端WCF服务上使用AppPool用户模拟用户?,c#,asp.net-mvc,wcf,windows-authentication,impersonation,C#,Asp.net Mvc,Wcf,Windows Authentication,Impersonation,我们有一个运行在.NET 4.5.2上的ASP.NET MVC应用程序,该应用程序托管在Windows 10或服务器2012R2上的IIS 7.5上 我如何允许用户使用其Windows帐户(使用Windows身份验证模式)登录并模拟他们,以便服务帐户(在应用程序池中配置)能够正常运行通过WCF服务而不是原始用户进行后端身份验证/授权?过了一段时间,我终于找到了一个有效的答案和解决方案 起初,IIS似乎只能向应用程序传递一个用户。如果打开MVC应用程序的配置编辑器并打开system.webServ

我们有一个运行在.NET 4.5.2上的ASP.NET MVC应用程序,该应用程序托管在Windows 10或服务器2012R2上的IIS 7.5上


我如何允许用户使用其Windows帐户(使用Windows身份验证模式)登录并模拟他们,以便服务帐户(在应用程序池中配置)能够正常运行通过WCF服务而不是原始用户进行后端身份验证/授权?

过了一段时间,我终于找到了一个有效的答案和解决方案

起初,IIS似乎只能向应用程序传递一个用户。如果打开MVC应用程序的配置编辑器并打开
system.webServer/serverRuntime
部分,您将看到
authenticatedUserOverride
has
UseAuthenticatedUser
UserWorkerProcessUser
。这将确定哪个用户被发送到MVC,您将在MVC应用程序的控制器中的
user.Identity
属性下找到
WindowsIdentity
。这是行不通的

我偶然发现了一篇来自Microsoft()的文章,这篇文章为我的工作奠定了基础,并由此引出了解决方案

在这篇文章中,他们提到您需要使用
Impersonate()
调用,该调用在客户端上使用如下内容:

使用(((WindowsIdentity)User.Identity.Impersonate())
{
var channelFactory=新的channelFactory(“*”);
var proxy=channelFactory.CreateChannel();
返回视图((对象)proxy.GetName(“1”);
}
在服务端,您需要将您的操作标记为模拟:

[操作行为(模拟=模拟选项。必选)]
我还添加了
PrincipalPermission
,以确保只有服务帐户通过:

[PrincipalPermission(SecurityAction.Demand,Name=“PC-Name\\svccount”)]
而且您似乎需要使用
wsHttpBinding
绑定


我让它通过SSL工作,它自动将凭据传递给WCF服务,但它是原始用户,而不是服务帐户,因此我从服务中得到了授权错误

在进一步的谷歌搜索中,我发现了这个片段,它给了我一个想法:

我将客户端代码替换为如下所示:

使用(System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate())
{
var channelFactory=新的channelFactory(“*”);
var proxy=channelFactory.CreateChannel();
返回视图((对象)proxy.GetName(“1”);
}
那你瞧!成功了!服务授权了用户(我更改了属性中的名称以确保我没有做梦),并返回了一个值,前端UI仍然显示登录的用户