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

时间:2014-09-02 15:01:38

标签: c# wcf kerberos

给定的是一个wcf rest服务,它使用HttpClientCredentialType.Windows运行并强制用户通过kerberos进行身份验证。

        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/时,我收到了一个错误的请求&#39;响应。 我确实启用了kerberos日志记录,它显示了我KDC_ERR_PREAUTH_REQUIRED

我可以通过创建Windows服务来解决此问题,并在“本地系统帐户”下运行它。 在这种情况下,客户端可以进行身份​​验证。

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

2 个答案:

答案 0 :(得分:3)

现在正在运作。 这真的是SPN的一个问题 一开始,我已将SPN设置为 setpn -A HTTP / notebook50.foo.com ,因此,kerberos身份验证无效。

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

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

如果可以解释这里发生的事情,并且可能是这个场景的文档链接,那将是很好的。

答案 1 :(得分:0)

您可以通过在Active Directory用户&amp;中为该用户帐户启用“不要求Kerberos预身份验证”选项来阻止此错误弹出。电脑 - &gt;属性 - &gt;帐户。