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

Man*_*uel 5 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();
}
Run Code Online (Sandbox Code Playgroud)

当我将其作为控制台应用程序运行,然后http://notebook50:87/test/从另一台计算机打开Internet Explorer中的网站时,我收到"错误请求"响应.我确实启用了kerberos日志记录,它显示了KDC_ERR_PREAUTH_REQUIRED

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

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

Man*_*uel 3

现在正在工作。这确实是 SPN 的问题。一开始,我设置了 SPN,如setpn -A HTTP/notebook50.foo.com,这样,kerberos 身份验证就不起作用了。

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

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

如果有人能够解释这里发生的情况,并且可能有指向此场景的文档的链接,那就太好了。