每次我必须使用IIS7为ASP.NET添加处理程序或模块时,指令总是告诉我将它合并为两个部分:system.web和system.webserver.
<system.web>
<httpHandlers>
</httpHandlers>
<httpModules>
</httpModules>
</system.web>
Run Code Online (Sandbox Code Playgroud)
还有这个:
<system.webServer>
<modules>
</modules>
<handlers>
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
这两个部分有什么区别?
此外,如果我不将它添加到该system.web部分,我的Visual Studio 2008调试器也无法正常工作.
我有一个在Windows服务中托管的WCF REST服务,我希望每次响应都发送Access-Control-Allow-Origin HTTP标头(定义为CORS的一部分).
我尝试的解决方案是在IDispatchMessageInspector实现中使用以下内容:
public void BeforeSendReply(ref Message reply, object correlationState)
{
var httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
if (httpResponse != null)
{
// test of CORS
httpResponse.Headers["Access-Control-Allow-Origin"] = "*";
}
}
Run Code Online (Sandbox Code Playgroud)
通常这可以工作,但不幸的是我的服务也使用HTTP基本授权,这意味着当请求进入而没有Authorization标头时,WCF会自动发送401响应,要求提供凭据.遗憾的是,在此初始交换期间,WCF不会调用我的IDispatchMessageInspector,因此Access-Control-Allow-Origin标头不会添加到初始交换中.
当我尝试从浏览器调用服务时,会出现此问题.CORS指定仅当源域与Access-Control-Allow-Origin响应头中列出的域匹配时才允许跨源请求(*匹配所有域).不幸的是,当浏览器看到没有Access-Control-Allow-Origin标头的初始401响应时,它会阻止访问(根据相同的源策略).
有没有办法在WCF自动发送的初始401响应中添加标头?
我在WCF 4.0中实现了很多RESTful(GET和POST)方法.所有这些都通过SSL工作.
一些方法的示例:
[OperationContract]
[WebInvoke(UriTemplate = "Login?", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
LoginResponse Login(LoginRequest request);
[OperationContract]
[WebInvoke(UriTemplate = "UpdateDetails?", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
UpdateUserDetailResponse UpdateDetails(UpdateUserDetailRequest request);
[OperationContract]
[WebInvoke(UriTemplate = "GetDetails?", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
UserDetailResponse GetDetails(UserDetailRequest request);
Run Code Online (Sandbox Code Playgroud)
我查看了很多博客和论坛,但仍然无法找到符合我要求的内容.我需要对某些方法实现基本身份验证,但不是全部.如果你看一下上面的例子,我需要为UpdateDetails和GetDetails方法发送用户名和密码,而不是Login方法.然后,对数据库验证用户名和密码.可以这样做吗?
作为旁注:这些REST方法由许多不同的移动设备调用.
我查看了以下站点,它们都通过REST实现基本身份验证,但它们涵盖了上面提到的所有方法.
有可能做我想做的事吗?