[英]Delegation not working
我正在尝试让委派使用WebClient(或HttpClient)来调用Web服务。 我有一个带有控制器和Web Api控制器的MVC4 Web应用程序。 我在Intranet上的远程服务器上设置了应用程序,并且在两台计算机上都使用IIS,并且使用了基本身份验证,在本地计算机上运行了相同的应用程序。
Web Api控制器很简单,这是该服务的代码:
[Authorize(Users="domain\\username")]
[HttpPost]
[HttpGet]
public string GetSimulationTest()
{
return "WS successfully called";
}
从我的浏览器或提琴手(无论本地还是远程),此WS都可以正常工作。
在我的家庭控制器中,我有以下方法:
public ActionResult TestOwnWS()
{
ContentResult res = null;
WindowsIdentity wi = (WindowsIdentity)User.Identity;
WindowsImpersonationContext ctx = null;
try
{
ctx = wi.Impersonate();
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
wc.Headers.Add("Content-Type", "application/xml");
res = this.Content(wc.DownloadString("linktoWS"), "application/xml");
}
catch (Exception e)
{
Response.Write(e.Message);
Response.End();
}
finally
{
ctx.Undo();
}
return res;
}
问题出在这里:我在wc.DownloadString()调用中得到401 Unauthorized。 如果我使用本地Web服务而不是远程Web服务,则什至不起作用。 但是,如果我使用wc.Credentials = new NetworkCredentials(user,pass,domain);
手动设置wc.Credentials wc.Credentials = new NetworkCredentials(user,pass,domain);
有用。
我以前使用过Windows身份验证,但仍然无法正常工作,因此我仔细阅读了委派,如果Windows身份验证更为精巧,但如果两台计算机(它们)上的帐户均具有基本身份验证相同,则它应该可以正常工作。
为什么拒绝使用默认凭据,我是否仍会错误地委托? 我已经阅读了有关msdn的文章,但找不到我做错的事情。
授权是允许某些用户访问操作方法的过滤器。
WindowsIdentity wi = (WindowsIdentity)User.Identity;
上面的代码仍然获得登录用户的身份。 模拟不是正确的方法。 模拟代码要求您使用模拟用户登录,并使用句柄获取Windows身份。
最好的方法是使用您提到的NetworkCredentials。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.