我有一个自托管的REST WCF Windows服务。我已经让服务支持基本认证,但我也想支持客户端使用Windows身份验证。我需要在不同的端口上拥有单独的终结点吗?
更新:我已经在WCF 4.0上实现了类似的功能。以下是代码,我现在遇到的问题是,我只能获得NTLM工作,这需要用户输入其凭据,这使使用Windows Auth无法发挥任何好处。
我仍然不确定如何在不要求用户再次输入密码的情况下启用Windows身份验证。
更新:我已经在WCF 4.0上实现了类似的功能。以下是代码,我现在遇到的问题是,我只能获得NTLM工作,这需要用户输入其凭据,这使使用Windows Auth无法发挥任何好处。
我仍然不确定如何在不要求用户再次输入密码的情况下启用Windows身份验证。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.IdentityModel.Selectors;
namespace BasicAndNegotiateAuth
{
class Program
{
static void Main(string[] args)
{
Uri newUri = new Uri(new Uri("http://localhost/"), "/");
WebServiceHost webHost = new WebServiceHost(typeof(HelloWorldService), newUri);
// TransportCredentialOnly means we can use http
WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic | HttpClientCredentialType.Ntlm;
ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(IHelloWorld), binding, newUri);
WebHttpBehavior wb = new WebHttpBehavior();
ep.EndpointBehaviors.Add(wb);
ep.Behaviors.Add(new WebHttpCors.CorsSupportBehavior());
//ServiceAuthenticationBehavior sab = null;
//sab = webHost.Description.Behaviors.Find<ServiceAuthenticationBehavior>();
//if (sab == null)
//{
// sab = new ServiceAuthenticationBehavior();
// sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.IntegratedWindowsAuthentication;
// host.Description.Behaviors.Add(sab);
//}
webHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
webHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNameValidator();
webHost.Open();
Console.ReadLine();
}
}
public class CustomUserNameValidator: UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
int i = 1;
}
}
[ServiceContract]
public interface IHelloWorld
{
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(
UriTemplate = "/",
ResponseFormat = WebMessageFormat.Json)]
string GetHello();
}
public class HelloWorldService : IHelloWorld
{
public string GetHello()
{
ServiceSecurityContext ssc = ServiceSecurityContext.Current;
return "Hello World";
}
}
}
AuthenticationSchemes.IntegratedWindowsAuthentication
是您设置 Windows 身份验证 所需的内容。是的,这仅适用于 .NET 4.5 及更高版本。 - Derek WUserNamePasswordValidator
处理。类似于为其他绑定指定ClientCredentialType
的UserName
时。 - Derek W