【发布时间】:2015-10-03 14:57:49
【问题描述】:
我的问题是我的服务只允许我自己的 Windows 帐户连接到它,但我想允许来自任何有效 Windows 域帐户的连接。我的服务托管在 IIS Express 中。当我在 Google Chrome 中浏览到 https://localhost:44300/FileRetrievalService.svc/get 时,我看到我的 [WebGet] 方法被调用得很好。当我尝试向它创建网络请求时,它也可以工作,只要我指定自己的 Windows 用户帐户:
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Path", filePath);
request.Credentials = new NetworkCredential("username", "password", "localhost");
return request.GetResponse();
当我尝试在我的机器上指定一个不同的本地 Windows 帐户,通过将上述代码中的 "username" 和 "password" 更改为我确定我的机器上存在的另一个帐户来验证用户身份时,我得到了以下错误:
System.Net.WebException:远程服务器返回错误:(401) 未经授权。
我的服务Web.config 中有以下内容:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithTransportSecurity">
<security mode="Transport">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="FileRetrievalPoCV3.FileRetrievalService">
<endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithTransportSecurity" contract="FileRetrievalPoCV3.IFileRetrieval" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="false"/>
</system.webServer>
</configuration>
这个问题的原因是什么?
【问题讨论】:
-
我猜你的帐户有管理员权限,而另一个帐户没有。如果是这种情况,您需要明确授予项目文件夹的权限(即使用 Windows 资源管理器)或将该用户分配给已经拥有权限的组。
-
@Keith 你是对的,我的项目文件夹没有我的非管理用户的权限。我为该帐户所属的
Users组添加了权限,它让连接发生。但是,我觉得这有点奇怪,因为当我创建一个基本上做同样事情的程序化服务时,它开箱即用(stackoverflow.com/questions/31326727/…)。唯一的区别是我现在托管在 IIS Express 中。为什么会这样?因为 IIS? -
@Keith 哦,我现在明白为什么了。作为 IIS Express 和可托管的 IIS 站点,服务文件需要实际提供给用户,这意味着 IIS Express 和 IIS 使用反射来尝试使用该用户的权限获取文件。我现在明白了。
-
顺便说一句,如果有人感兴趣,我所需要的只是该用户帐户对 bin 文件夹中我的服务 .svc 文件的读取权限。
标签: c# asp.net .net server webrequest