WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题
摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication, 但是仍然出现IIS没有启用Windows Authentication的问题. 在网络上能查到的资料很少. 通过自己的troubleshooting发现所遇到的错误提示比较具有迷惑性. 所以POST上来给大家分享一下.
问题 :
最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication, 但是仍然出现IIS没有启用Windows Authentication的问题. 然而在启动这个WCF Service的时候遇到了如下的错误.
Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NotSupportedException: Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service.]
System.ServiceModel.Channels.HttpChannelListener.ApplyHostedContext(VirtualPathExtension virtualPathExtension, Boolean isMetadataListener) +15710645
System.ServiceModel.Channels.HttpsChannelListener.ApplyHostedContext(VirtualPathExtension virtualPathExtension, Boolean isMetadataListener) +27
System.ServiceModel.Channels.HttpsTransportBindingElement.BuildChannelListener(BindingContext context) +105
System.ServiceModel.Channels.BindingContext.BuildInnerChannelListener() +95
System.ServiceModel.Channels.MessageEncodingBindingElement.InternalBuildChannelListener(BindingContext context) +102
System.ServiceModel.Channels.TextMessageEncodingBindingElement.BuildChannelListener(BindingContext context) +70
System.ServiceModel.Channels.BindingContext.BuildInnerChannelListener() +95
System.ServiceModel.Channels.Binding.BuildChannelListener(Uri listenUriBaseAddress, String listenUriRelativeAddress, ListenUriMode listenUriMode, BindingParameterCollection parameters) +166
System.ServiceModel.Description.DispatcherBuilder.MaybeCreateListener(Boolean actuallyCreate, Type[] supportedChannels, Binding binding, BindingParameterCollection parameters, Uri listenUriBaseAddress, String listenUriRelativeAddress, ListenUriMode listenUriMode, ServiceThrottle throttle, IChannelListener& result, Boolean supportContextSession) +399
System.ServiceModel.Description.DispatcherBuilder.BuildChannelListener(StuffPerListenUriInfo stuff, ServiceHostBase serviceHost, Uri listenUri, ListenUriMode listenUriMode, Boolean supportContextSession, IChannelListener& result) +499
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +1937
System.ServiceModel.ServiceHostBase.InitializeRuntime() +61
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +63
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +135
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +654 [ServiceActivationException: The service '/WinAuthService.svc' cannot be activated due to an exception during compilation. The exception message is: Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service..]
System.ServiceModel.AsyncResult.End(IAsyncResult result) +15786048
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +15706393
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +265
System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +227
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
下面是这个WCF Service的配置. 按照这个配置, 是需要在IIS上启用Windows Authentication.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBasicBinding">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WCFSample.WinAuthService">
<endpoint address="WinAuthService.svc" binding="basicHttpBinding"
bindingConfiguration="myBasicBinding" contract="WCFSample.IWinAuthService" />
</service>
</services>
</system.serviceModel>
在IIS的管理界面上, 已经按照启用了Windows Authentication并且禁用了Anonymous Authentication.
分析 :
报错信息的最顶上一条已经提示了抛出这个异常的CALL STACK 是 System.ServiceModel.Channels.HttpChannelListener.ApplyHostedContext. 因为是托管代码, 所以用工具ILSPY来检查这段代码看什么情况下会抛出这样的错误. 从代码上看, 这个异常是在做了一定逻辑的校验之后, 主动抛出来.
internal override void ApplyHostedContext(VirtualPathExtension virtualPathExtension, bool isMetadataListener)
{
ServiceNameCollection customServiceNames;
base.ApplyHostedContext(virtualPathExtension, isMetadataListener);
AuthenticationSchemes authenticationSchemes = HostedTransportConfigurationManager.MetabaseSettings.GetAuthenticationSchemes(base.HostedVirtualPath);
if (this.AuthenticationScheme == AuthenticationSchemes.Anonymous && (authenticationSchemes & AuthenticationSchemes.Anonymous) == AuthenticationSchemes.None && isMetadataListener)
{
if ((authenticationSchemes & AuthenticationSchemes.Negotiate) == AuthenticationSchemes.None)
{
this.authenticationScheme = authenticationSchemes;
}
else
{
this.authenticationScheme = AuthenticationSchemes.Negotiate;
}
}
if ((authenticationSchemes & this.AuthenticationScheme) == AuthenticationSchemes.None)
{
if (!AuthenticationSchemesHelper.IsWindowsAuth(this.AuthenticationScheme))
{
ExceptionUtility exceptionUtility = DiagnosticUtility.ExceptionUtility;
object[] str = new object[] { this.AuthenticationScheme.ToString() };
throw exceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString("Hosting_AuthSchemesRequireOtherAuth", str)));
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString("Hosting_AuthSchemesRequireWindowsAuth")));
}
if (this.AuthenticationScheme != AuthenticationSchemes.Anonymous)
{
ExtendedProtectionPolicy extendedProtectionPolicy = HostedTransportConfigurationManager.MetabaseSettings.GetExtendedProtectionPolicy(base.HostedVirtualPath);
if (extendedProtectionPolicy == null)
{
if (this.extendedProtectionPolicy.PolicyEnforcement == PolicyEnforcement.Always)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString("ExtendedProtectionNotSupported")));
}
}
else if (!isMetadataListener || !ChannelBindingUtility.IsDefaultPolicy(this.extendedProtectionPolicy))
{
ChannelBindingUtility.ValidatePolicies(extendedProtectionPolicy, this.extendedProtectionPolicy, true);
if (this.usingDefaultSpnList)
{
customServiceNames = null;
}
else
{
customServiceNames = this.extendedProtectionPolicy.CustomServiceNames;
}
if (!ChannelBindingUtility.IsSubset(extendedProtectionPolicy.CustomServiceNames, customServiceNames))
{
object[] objArray = new object[] { SR.GetString("Hosting_ExtendedProtectionSPNListNotSubset") };
string str1 = SR.GetString("Hosting_ExtendedProtectionPoliciesMustMatch2", objArray);
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(str1));
}
}
else
{
this.extendedProtectionPolicy = extendedProtectionPolicy;
}
}
if (!ServiceHostingEnvironment.IsSimpleApplicationHost)
{
this.realm = HostedTransportConfigurationManager.MetabaseSettings.GetRealm(virtualPathExtension.VirtualPath);
}
}
不过由于不是我们确切看到的错误. 所以需要在DLL的Resources中确切的验证一下. 在这里明确
Hosting_AuthSchemesRequireWindowsAuth=Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service.
往上判断, 能够进入到 if ((authenticationSchemes & this.AuthenticationScheme) == AuthenticationSchemes.None) . AuthenticationSchemes是一个枚举类型. 在这里注意到了有两个分开的定义, Negotiate和Ntlm. 在 IIS Windows Authentication 可以配置多个Provider. 默认情况下有2个, 分别是Negotiate 和 NTLM.
namespace System.Net
{
/// <summary>Specifies protocols for authentication.</summary>
[Flags]
public enum AuthenticationSchemes
{
/// <summary>No authentication is allowed. A client requesting an <see cref="T:System.Net.HttpListener" /> object with this flag set will always receive a 403 Forbidden status. Use this flag when a resource should never be served to a client.</summary>
None = ,
/// <summary>Specifies digest authentication.</summary>
Digest = ,
/// <summary>Negotiates with the client to determine the authentication scheme. If both client and server support Kerberos, it is used; otherwise, NTLM is used.</summary>
Negotiate = ,
/// <summary>Specifies NTLM authentication.</summary>
Ntlm = ,
/// <summary>Specifies Windows authentication.</summary>
IntegratedWindowsAuthentication = ,
/// <summary>Specifies basic authentication. </summary>
Basic = ,
/// <summary>Specifies anonymous authentication.</summary>
Anonymous =
}
}
在得到这些线索之后, 检查了Windows Authentication的Providers. 果然只有一个NTLM. 添加了一个新的Negotiate 的 Provider之后, WCF Service就得到了解决.
结论 :
这篇文章中仅讨论其中一种可能造成这样问题的情况. 这里我遇到的问题与Windows Authentication的Provider有关系.
当 WCF Service 上启用了Transport 层面上的安全设定之后, 可以配置某一种类型的ClientCredentialType. 其中可以包括 None, Basic, Digest, Ntlm, Windows, Certificate 和 Password.
当指定为Windows的时候, 实质是要求Kerboer或者NTLM两者皆可. Server先去尝试Kerberos验证, 如果Kerberos验证失败, 则会尝试通过NTLM. 也可以通过设置另外的属性 AllowNtlm为false来强制使用Kerberos. Kerberos和Ntlm是两种不同的验证方式.
由于这一点的不同, 在IIS上要确保做出相应的配置. 即, 开启IIS的Windows Authentication的同时, 要确保Negotiate Provider也在列表中. 只有Ntlm会被认为是错误的配置.
可以参考这里的链接 :
- https://msdn.microsoft.com/library/ms733836%28v=vs.110%29.aspx
- http://blogs.msdn.com/b/benjaminperkins/archive/2011/09/14/iis-integrated-windows-authentication-with-negotiate.aspx
Sonic Guo
WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题的更多相关文章
- [Windows Azure] Create and use a reporting service in Windows Azure SQL Reporting
Create and use a reporting service in Windows Azure SQL Reporting In this tutorial you will learn ab ...
- 客户端通过wcf来启动或者停止服务器上的windows service
1.设置服务器上的windows service的security,下面的命令只能用cmd.exe来运行(以管理员模式) sc sdset "LISA_43_Dev_Batch" ...
- Windows 7下安装MySQL Server卡在Apply Security Settings的解决方案(转)
如果操作无效,请卸载MySQL Server后换一个位置安装 例如默认的是C:\Program Files\MySQL 安装时选Custom修改到D:\Program Files\MySQL试试 == ...
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...
- MySQL安装最后一步apply security settings错误
网上查了很久都是说删除各种文件什么的,直接百度apply security settings,说是mysql没卸载干净.不是的. 看日志发现 You must SET PASSWORD before ...
- MySQL安装问题:Unable to update security settings解决方案
主要问题还是之前装过,卸载的时候卸载不干净导致的. 如下: 安装到最后出现: Unable to update security settings. Access denied for user 'r ...
- the security settings could not be applied to the database(mysql安装error)【简记】
在安装mysql时,出现“The security settings could not be applied to the database because the connection has f ...
- How to resolve "your security settings have blocked an untrusted application from running" in Mac
If you encounter the error "your security settings have blocked an untrusted application from r ...
- Mysql安装过程中出现apply security settings错误的解决方法
在学习Mysql的过程中,首先要安装Mysql.然而在第一遍安装过程中难免会出现安装错误的时候,当卸载后第二次安装(或者第三次甚至更多次)的时候,往往在安装最后一步会出现apply security ...
随机推荐
- AutoMapper随笔记
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill 先看效果:(完整Demo:https://git ...
- Android-armebi-v7a、arm64-v8a、armebi的坑
先来一波扫盲: armeabi:针对普通的或旧的arm v5 cpu armeabi-v7a:针对有浮点运算或高级扩展功能的arm v7 cpu(32位ARM设备) arm64-v8a:64位ARM设 ...
- SQL Server-聚焦使用视图若干限制/建议、视图查询性能问题,你懵逼了?(二十五)
前言 上一节我们简单讲述了表表达式的4种类型,这一系列我们来讲讲使用视图的限制,简短的内容,深入的理解,Always to review the basics. 避免在视图中使用ORDER BY 上一 ...
- .Net 大型分布式基础服务架构横向演变概述
一. 业务背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 二. 基础 ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
- IIC驱动移植在linux3.14.78上的实现和在linux2.6.29上实现对比(deep dive)
首先说明下为什么写这篇文章,网上有许多博客也是介绍I2C驱动在linux上移植的实现,但是笔者认为他们相当一部分没有分清所写的驱动时的驱动模型,是基于device tree, 还是基于传统的Platf ...
- PHP 获取 特定时间范围 类
目录 前序 用途 功能及事项 使用方法 代码及注释 前序: 总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP ,我也不会学这么多,这 2年来, ...
- SQL Server存储过程
创建于2016-12-24 16:12:19 存储过程 概念: 1.存储过程是在数据库管理系统中保存的.预先编译的.能实现某种功能的SQL程序,它是数据库应用中运用比较广泛的 一种数据对象. 2.存储 ...
- linux yum命令详解
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...
- jetBrain系列软件
请尽量支持正版软件!https://www.jetbrains.com/ 本文仅供参考 以下提供一种方法可以无限期体验JetBrain2016系列软件. 1.下载JetbrainsCrack-2.5. ...