WCF Rest服务通过浏览器进行Windows身份验证

问题描述:

鉴于wcf rest服务与HttpClientCredentialType.Windows一起运行并强制用户通过Kerberos进行身份验证。WCF Rest服务通过浏览器进行Windows身份验证

 private static void Main(string[] args) 
    { 
     Type serviceType = typeof (AuthService); 
     ServiceHost serviceHost = new ServiceHost(serviceType); 

     WebHttpBinding binding = new WebHttpBinding(); 
     binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly; 
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 

     ServiceEndpoint basicServiceEndPoint = serviceHost.AddServiceEndpoint(typeof(IAuthService), binding, "http://notebook50:87"); 
     basicServiceEndPoint.Behaviors.Add(new WebHttpBehavior()); 

     Console.WriteLine("wcf service started"); 
     serviceHost.Open(); 
     Console.ReadLine(); 
    } 

    public class AuthService : IAuthService 
{ 
    public List<string> GetUserInformation() 
    { 
     List<string> userInfo = new List<string>(); 
     userInfo.Add("Environment.User = " + Environment.UserName); 
     userInfo.Add("Environment.UserDomain = " + Environment.UserDomainName); 
     if (OperationContext.Current != null && OperationContext.Current.ServiceSecurityContext != null) 
     { 
      userInfo.Add("WindowsIdentity = " + OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name); 
      userInfo.Add("Auth protocol = " + OperationContext.Current.ServiceSecurityContext.WindowsIdentity.AuthenticationType); 
     } 
     else 
     { 
      userInfo.Add("WindowsIdentity = empty"); 
     } 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; 
     return userInfo; 
    } 
} 

[ServiceContract] 
public interface IAuthService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "test/")] 
    List<string> GetUserInformation(); 
} 

当我运行这是一个控制台应用程序,然后在Internet Explorer中从另一台计算机打开网页http://notebook50:87/test/,我得到一个“错误的请求”的响应。 我确实启用了kerberos日志记录,它显示我KDC_ERR_PREAUTH_REQUIRED

我可以通过创建一个windows服务来解决这个问题,并在'本地系统帐户'下运行它。 在这种情况下,客户端能够进行身份验证。

问题:用户(运行此wcf服务)需要什么权限/设置才能获得与应用程序在本地系统下以windows服务运行时相同的行为? 这与服务原则名称有关吗?

+0

如果您在笔记本上创建共享文件夹50,并尝试从其他计算机访问它,会发生什么情况?它是否提示登录?如果您使用登录表单登录,然后尝试访问http:// notebook50:87/test /,那么它会起作用吗? – 2014-09-05 06:44:30

+0

“这与服务主体名称相关”很可能。我的第一个问题是为什么你在使用Kerberos,我过去花了两个星期的时间无情地调试Kerberos(你确​​定你不能使用NTLM)。其次,Kerberos需要大量的东西才能正常工作,其中之一就是客户端需要对服务器进行身份验证......这就是SPN的功能。 SPN必须与你用于访问服务器的DNS条目相匹配(在这种情况下,notebook50几乎可以肯定不是,默认情况下,如果发生这种情况,它将被设置为服务器的FQDN)。 – Aron 2014-09-05 06:47:08

+1

@ Magic-Mouse对不起老兄。您的评论没有帮助。这显然是Kerberos身份验证的一个问题。 – Aron 2014-09-05 06:48:04

它现在正在工作。 它确实是SPN的问题 在开始时,我已将SPN设置为setpn -A HTTP/notebook50.foo.com,因此,kerberos身份验证不起作用。

现在,我已将它设置为setspn -A HTTP/notebook50.foo.com用户名其中username是运行该服务的用户。

从我读过的SPN文档中,我不清楚我必须以这种方式设置用户帐户。

如果能够解释此处发生的情况并且可能是此场景文档的链接,那将是非常棒的。

您可以通过在Active Directory用户& computers - > properties - >帐户中为该用户帐户启用“不要求Kerberos预身份验证”选项来阻止此错误。

+0

我确实为用户帐户启用了此属性正在运行Windows服务。它仍然不起作用。现在系统日志中的错误是:KRB_AP_ERR_MODIFIED。但只要我将用户切换到“本地系统”,一切正常。 – Manuel 2014-09-08 06:40:38

+0

根据msdn规范,在下列情况下KRB_AP_ERR_MODIFIED可能会出现:1。不匹配DNS名称解析 - 在使用乘法 IP或/和乘法网络适配器的NLB环境中,此问题非常普遍。 2.用户没有本地NTFS访问权限。 3.网站使用应用程序池的权限设置较差。 – Frix33 2014-09-08 06:56:06

+0

检查此kb是否可以帮助您:[link](http://support.microsoft.com/kb/558115) – Frix33 2014-09-08 06:59:32